Python indentation in "empty lines" Python indentation in "empty lines" python python

Python indentation in "empty lines"


If you use A, you could copy paste your block in python shell, B will get unexpected indentation error.


The PEP 8 does not seem to be clear on this issue, although the statements about "blank lines" could be interpreted in favor of B. The PEP 8 style-checker (pep8.py) prefers B and warns if you use A; however, both variations are legal. My own view is that since Python will successfully interpret the code in either case that this doesn't really matter, and trying to enforce it would be a lot of work for very little gain. I suppose if you are very adamantly in favor of one or the other you could automatically convert the one to the other. Trying to fix all such lines manually, though, would be a huge undertaking and really not worth the effort, IMHO.


Adding proper indentation to blank lines (style A in the question) vastly improves code readability with display whitespace enabled because it makes it easier to see whether code after a blank line is part of the same indentation block or not.

For a language like Python, where there is no end statement or close bracket, I'm surprised this is not part of PEP. Editing Python with display whitespace on is strongly recommended, to avoid both trailing whitespace and mixed indentation.

Compare reading the following:

A)

def foo():....x = 1....y = 2........if True:........bar()

B)

def foo():....x = 1....y = 2....if True:........bar()

In A, it is far clearer that the last two lines are part of foo. This is even more useful at higher indentation levels.