简体   繁体   中英

Python indentation in “empty lines”

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?

如果您使用A ,您可以将您的块复制粘贴到 python shell 中, B会出现意外的缩进错误。

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.

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.

如果您使用 B,TextMate 会破坏块折叠,无论如何我更喜欢 A,因为它更“合乎逻辑”。

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. Eg Visual Studio (at least 2008) automatically prevents this from happening without using any whitespace characters on those lines.

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.

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.

B is preferred - ie no indentation. PEP 8 says :

Avoid trailing whitespace anywhere. Because it's usually invisible, it can be confusing: eg a backslash followed by a space and a newline does not count as a line continuation marker. Some editors don't preserve it and many projects (like CPython itself) have pre-commit hooks that reject it.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM