Python 中的“空行”缩进

发布于 2024-08-30 13:00:12 字数 370 浏览 3 评论 0原文

哪个是首选(“.”表示空格)?

A)

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

B)

def foo():
    x = 1
    y = 2

    if True:
        bar()

我的直觉是 B (这也是 Vim 所做的我),但我看到人们一直在使用 A)。难道只是因为大多数编辑器都坏了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

遥远的绿洲 2024-09-06 13:00:12

如果您使用 A,您可以在 Python shell 中复制粘贴您的块,但 B 将收到意外缩进错误(“IndentationError:意外缩进”)。

If you use A, you could copy paste your block in Python shell, but B will get an unexpected indentation error ("IndentationError: unexpected indent").

|煩躁 2024-09-06 13:00:12

PEP 8 在这个问题上似乎并没有明确,尽管声明关于“空行”可以解释为有利于 B。PEP 8 样式检查器 (pep8.py) 更喜欢 B,并在您使用 A 时发出警告;然而,这两种变体都是合法的。我自己的观点是,由于 Python 在任何一种情况下都会成功解释代码,因此这并不重要,并且尝试强制执行它会付出大量工作而收效甚微。我想如果你非常坚定地支持其中之一,你可以自动将其中一种转换为另一种。不过,尝试手动修复所有这些线路将是一项艰巨的任务,而且真的不值得付出努力,恕我直言。

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.

请止步禁区 2024-09-06 13:00:12

向空行添加适当的缩进(问题中的样式 A)在启用显示空白的情况下极大地提高了代码的可读性,因为它可以更轻松地查看空行之后的代码是否属于同一缩进块的一部分。

对于像 Python 这样没有结束语句或右括号的语言,我很惊讶这不是 PEP 的一部分。强烈建议在打开显示空白的情况下编辑 Python,以避免尾随空白和混合缩进。

比较阅读以下内容:

A)

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

B)

def foo():
....x = 1
....y = 2

....if True:
........bar()

A 中,最后两行是 foo 的一部分,这一点要清楚得多。这在更高的压痕级别上更加有用。

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.

隔岸观火 2024-09-06 13:00:12

该空行属于 foo(),因此我认为 A 是最自然的。但我想这只是一个意见问题。

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.

听不够的曲调 2024-09-06 13:00:12

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

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

得不到的就毁灭 2024-09-06 13:00:12

我在开源开发方面的经验是,永远不应该在空行内留下空格。另外,永远不应该留下尾随空白。

这是编码礼仪的问题。

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.

路还长,别太狂 2024-09-06 13:00:12

B 是优选的——即没有缩进。 PEP 8 说

避免任何地方出现尾随空格。因为它通常是不可见的,所以可能会令人困惑 [...] 一些编辑器不保留它,并且许多项目(如 CPython 本身)都有拒绝它的预提交钩子。


这些格式化程序强制执行此操作:

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.


These formatters enforce this:

泪眸﹌ 2024-09-06 13:00:12

我不一定将第一个示例称为“损坏”,因为我知道有些人讨厌在代码中向上或向下移动光标时光标“跳回”。例如,Visual Studio(至少 2008)会自动防止这种情况发生,而无需在这些行上使用任何空白字符。

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.

绮筵 2024-09-06 13:00:12

Emacs 对我来说是 B),但我真的认为这并不重要。 A) 意味着您可以在正确的缩进处添加一行,而无需任何制表符。

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.

情绪操控生活 2024-09-06 13:00:12

vi 隐含地阻止了 A 中的行为,因为 {/< code>} 导航不再按预期工作。 Git 在运行 git diff。我还认为,如果一行包含空格,那么它就不是空行。

出于这个原因,我强烈喜欢 B。没有什么比期望用 { 动作跳过六行左右并最终到达类定义的顶部更糟糕的了。

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.

最美不过初阳 2024-09-06 13:00:12

除了@Zags 答案。添加此作为答案以便能够附加图片。

如果您希望 PyCharm 使用“A”样式(在空行上保留缩进),请在设置中启用它:
文件 |设置 |编辑|代码风格| Python
在您选择的方案中,启用复选框“在空行上保留缩进”。

屏幕截图显示settings

但是,对于现有的不正确(样式“B”)文件,您仍然需要触发重新格式化。从主菜单:代码 |重新格式化文件CtrlAltShiftL)。

In addition to @Zags answer. Adding this as an answer to be able to attach picture.

If you want PyCharm to use the "A" style (keep indents on empty lines), enable it in settings:
File | Settings | Editor | Code Style | Python.
On the scheme of your choice, enable checkbox "Keep indents on empty lines".

screenshot showing settings

However, for existing incorrect (style "B") files, you still need to trigger reformatting. From the main menu: Code | Reformat File (CtrlAltShiftL).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文