Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.x multi line comment throws syntax error

I'm working on a Python project and as of now, my code has over 400+ lines. At one point, I had to write a multi-line comment about a small bug which needs a work around, and the interpreter decided to throw a syntax error.

According to the interpreter, the syntax error is occuring at elif. I re-checked my indentation, converted tabs to spaces etc. Nothing seems to work.

                    if some_condition_1 == True:
                       do_something()

                    """
                    Sub stage (b):
                    Refer documentation [1.7A] for ...
                    ....
                    ....
                    ....
                    """
                    elif condition_1 == True:
                        if condition_2 == False:
                            list.append(item)

However, if I remove the multi-line comment, the code executes fine.

Any idea what's going wrong? Please note that the code sample I've show above, is at very top of the file, and there's no chance for anything to go wrong elsewhere.


1 Answers

This is an indentation error. Your "multi-line comment" (really multi-line string) must be indented under the if block just like anything else.

""" These kinds of things """ are not really comments in Python. You're just creating a string and then throwing the value away (not storing it anywhere). Since Python doesn't have true multi-line comments, many people use them this way. However, since they are not true comments (they aren't ignored by the interpreter), they must obey all normal syntax rules, including indentation rules.

(Do note that when I say "creating a string" I'm speaking loosely. CPython, at least, has an optimization not to create an object here.)

like image 85
Two-Bit Alchemist Avatar answered Aug 06 '26 00:08

Two-Bit Alchemist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!