PEP 8 的 "no-alignment" 政策的动机是什么?

What motivates the "no-alignment" policy of PEP 8?

PEP 8 has a specific guideline 针对对齐操作符周围的空格:

No:

x             = 1
y             = 2
long_variable = 3

如果我没理解错的话,下面也建议反对:

salaries = {
    "Alice": 1500,
    "Bob":   1300,
    #      ^^ extra whitespace
}

这些具体建议有什么用?

此规则是 Guido 原创风格文章的一部分 published in 1998,他在其中指出:

(Don't bother to argue with me on any of the above -- I've grown accustomed to this style over 15 years.)

所以他没有直接激励这个,但我可以想出一些你不想这样做的好理由。将代码与 space 对齐可能 看起来 在当时是个好主意,一切都整齐地排列。

但大多数项目都不是静态的。您 经常 看到在现实生活中的项目代码会随着时间的推移而更改,并且您可能会在您如此仔细对齐的列表中添加、删除或重命名条目。您很可能最终不得不调整整个块的对齐方式,因为您的更改改变了现在最长的条目。

这样的变化现在会给您带来更多的工作。您必须重新对齐块。您的同事和您未来的自己必须做更多的工作来阅读版本控制系统中的变更集。

(是的,代码格式化程序可以进行重新对齐,但是自动化工具无法修复您的代码差异,现在触及大量与您的提交无关的行,除了重新对齐之外).

或者,后来的编辑可能会对必须重新对齐其他条目的前景犹豫不决。 Wine 项目 dlls/msi/msipriv.h 文件 started nicely aligned, but over time inconsistencies creep in and you end up with a bit of a mess.

接下来,对齐并不一定会使代码更易于阅读;中间有足够的白色-space,您很容易最终误读什么值与什么名称。

同时,PEP 8 是一个指南document itself states:

A Foolish Consistency is the Hobgoblin of Little Minds

[...]

However, know when to be inconsistent -- sometimes style guide recommendations just aren't applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best.

您需要自行决定何时使用对齐无论如何

Python标准库的某些部分break this specific rule (and yes, that's a Python reflection of the Wine project example above; that example was imported from an external project years ago), and there are other examples of broken PEP8 rules there for historical reasons,但有时在一些有限的区域,打破规则可以 有道理。