51

Which is preferred ("." indicating whitespace)?

A)

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

B)

def foo():
    x = 1
    y = 2

    if True:
        bar()

My intuition would be B (that's also what vim does for me), but I see people using A) all the time. Is it just because most of the editors out there are broken?

nisc
  • 4,222
  • 4
  • 29
  • 34

10 Answers10

52

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

YOU
  • 120,166
  • 34
  • 186
  • 219
  • I tried using A in idlex and got an invalid syntax error instead. – obesechicken13 Aug 11 '13 at 16:17
  • 3
    @obesechicken13 Those dots are supposed to be spaces, they're just drawn so that you know they're there. – pydsigner Aug 11 '15 at 20:52
  • 5
    When using IPython, you should paste with the `%paste` instead. It can handle either A or B. – erickrf Oct 06 '16 at 14:06
  • I had always intuitively used A before using git. As I started using git, it would display spaces on blank lines with red background in diffs, leading me to "fix" it by using B. Now I encountered the Python shell tripping over B, and have started to think "maybe git is wrong about this one". So, at least for Python projects, I have moved to using A again. To calm my diffs, I now use `git-so-fancy` with `diff-so-fancy.markEmptyLines` set to `false` and am experiencing no regrets. – Zyl Jan 24 '23 at 11:54
28

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.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • 18
    There are good arguments in favor of A -- copy into the shell, B leads to problems with some editors. Unless there are also issues with A it would seem to be a case of "A helps in some cases, hurts in none" and thus A should be used. – Ted Apr 17 '13 at 17:14
  • 1
    `s/^([\t ]+)([^\r\n]*)(\r?\n)\r?\n/\1\2\3\1\3/`. Search: Capture tabs or spaces at the beginning of a line, capture any non-newline characters if there are any, capture newline, find newline*. Replace: indentation, line content, newline, indentation, newline. Caveat: If the indentation line opens a new block, the regex doesn't detect this and simply puts the next line at the same indentation level. *I ignore the second newline format and just use the previous newline format to promote consistency – Chinoto Vokro Aug 16 '16 at 04:41
  • 2
    The pep8 linter complains on A. `W293 blank line contains whitespace` And many other python linters do the same – kmpm Feb 14 '19 at 22:07
  • PEP 8 is clear actually, it says "Avoid trailing whitespace anywhere". I posted [an answer](https://stackoverflow.com/a/61944534/4518341) myself. – wjandrea May 21 '20 at 22:04
  • Not sure if it's what you mean, but lots of editors support automatically trimming trailing whitespace, and you could easily write a script in `sed` to do the same. – wjandrea May 21 '20 at 22:09
  • Are they really "trailing whitespaces"? I mean, that really depends on how you define it. If you define it as "white spaces at the end of the line", then yeah. But is that really "rules as intended"? I would have interpreted "trailing whitespace" as something that.. well.. trails something. But if nothing is there, what does it trail? To me, the whitespaces that make indentation are not trailing whitespaces, even if they end up touching the end of the line. – RHS Mar 04 '21 at 12:40
  • @RHS I'm interpreting "trailing" as "at the end of the line" (in regex, `\s+$`). So I guess you could say it trails zero or more characters that make up the rest of the line (in regex, `^.*`). – wjandrea Mar 10 '22 at 19:36
  • 1
    @wjandrea yes, that's also what I described as one possible interpretation. I still think white spaces that are both trailing AND leading should be prioritized as leading white spaces (which make indentation) and not trailing for the sake of interpreting PEP8. That's because leading white spaces actually have syntactical relevance and trailing white spaces are relevant to cosmetic only. – RHS Mar 11 '22 at 07:43
21

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.

Nayuki
  • 17,911
  • 6
  • 53
  • 80
Zags
  • 37,389
  • 14
  • 105
  • 140
  • i aggressively agree with everything in this answer. and on top of that, the problem you outline is even worse if you have to understand some code that uses two-space tabs! – lefft Mar 24 '22 at 23:38
14

That empty line belongs to foo(), so I would consider A to be the most natural. But I guess it's just a matter of opinion.

T .
  • 4,874
  • 3
  • 23
  • 36
  • I agree that it's more natural, and I disagree it's a matter of opinion. It's a fact. – o0'. Aug 31 '14 at 13:58
6

TextMate breaks block collapsing if you use B, and I prefer A anyway since it's more "logical".

o0'.
  • 11,739
  • 19
  • 60
  • 87
  • 1
    Interesting. I just tried it with EditPadPro which also uses the level of indentation for code folding, and it handles empty lines well. – Tim Pietzcker Apr 28 '10 at 11:50
4

My experience in open-source development is that one should never leave whitespace inside blank lines. Also one should never leave trailing white-space.

It's a matter of coding etiquette.

wm_eddie
  • 3,938
  • 22
  • 22
  • 3
    @Lo'oris Indentation is mandatory for code but not for blank lines. Therefore any spaces in a blank line are unnecessary. – wm_eddie May 10 '10 at 06:32
  • 6
    And that's just yet another design flaw of python. "It's mandatory for lines containing codes _but not_ for empty lines" is plain stupid. – o0'. Aug 31 '14 at 13:59
  • 5
    @Lohoris I would have to disagree. Imagine how infuriating it would be to debug a language where an empty blank line caused completely different behaviour. It would be impossible to even see without using an advanced editor. – wm_eddie Sep 01 '14 at 00:19
  • 4
    In a language where intendation matters, I would expect a programmer both to notice that, and to have an advanced editor. – o0'. Sep 01 '14 at 08:10
  • This answer doesn't add anything useful whatsoever. Since in Python indentation is mandatory, PEP8 should favor indented empty lines over "empty" empty lines as well. Imho. – Semmel Mar 16 '17 at 01:30
  • you cannot expect to have that etiquette when you have a python on the table – Melvin Calvin Aug 24 '22 at 13:56
3

I wouldn't necessarily call the first example "broken", because I know some people hate it when the cursor "jumps back" when moving the cursor up or down in code. E.g. Visual Studio (at least 2008) automatically prevents this from happening without using any whitespace characters on those lines.

Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
3

B is preferred - i.e. no indentation. PEP 8 says:

Avoid trailing whitespace anywhere. Because it's usually invisible, it can be confusing [...] Some editors don't preserve it and many projects (like CPython itself) have pre-commit hooks that reject it.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
1

Emacs does B) for me, but I really don't think it matters. A) means that you can add in a line at the correct indentation without any tabbing.

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
1

vi implicitly discourages the behaviour in A because the {/} navigations no longer work as expected. git explicitly discourages it by highlighting it in red when you run git diff. I would also argue that if a line contains spaces it is not a blank line.

For that reason I strongly prefer B. There is nothing worse than expecting to skip six or so lines up with the { motion and ending up at the top of a class def.

Score_Under
  • 1,189
  • 10
  • 20