Python: I get the "indented" error in the last 3 quotation marks ("") of my function comments. What?

Super weird, no? Violation Code:

def main(): """ main function """ # Argument handling args = sys.argv[1:] if not args: print "usage is: ... 

The third quote is where I get the usual indent error:

 >>>Import someScript Traceback (most recent call last): File "<stdin>", line 1, in <module> File "someScript.py", line 24 """ ^ 

If I delete the comments (obviously, I don’t want to), then the next function to be defined gets the same error in the same place of its comments. If I remove all comments from functions, the error will disappear.

I do not understand! Why expect indentation? I write in Komodo Edit in part because it does not allow you to mix spaces and tabs, but in order to be sure that I have done a search, and of course there are no friggin tabs. Not that it would make sense anyway if they were.

What gives, guru?

+4
source share
2 answers

You need to reject the docstring string along with the block for your function.

Each colon ( : must immediately follow the indent.

+17
source

As said, the doxline has no indentation. It would be better to get an error in the first line of the line, but that is not how lexer works. Instead, it takes an entire token at a time β€” remember that triple-quoted strings imply a line β€” then throws an error if it is incorrect. This character represents the entire triple quotation mark that ends on another line. For comparison:

 >>> def f(): ... """one line""" File "<stdin>", line 2 """one line""" ^ IndentationError: expected an indented block >>> def f(): ... foo() File "<stdin>", line 2 foo() ^ IndentationError: expected an indented block >>> def f(): ... return 42 File "<stdin>", line 2 return 42 ^ IndentationError: expected an indented block 

Notice how in the second example it points to the end of "foo", the first character in this incorrect statement: this is the same as pointing to the end of your docstring.

+1
source

Source: https://habr.com/ru/post/1336418/


All Articles