问题:为什么PEP-8指定的最大行长为79个字符?[关闭]

为什么在这个千年中,Python PEP-8应该指定最大行长度为79个字符?

在阳光下几乎每个代码编辑器都可以处理更长的行。包装应该是内容消费者的选择,而不是内容创建者的责任。

在这个年龄段,是否有(合理的)充分理由坚持使用79个字符?

Why in this millennium should Python PEP-8 specify a maximum line length of 79 characters?

Pretty much every code editor under the sun can handle longer lines. What to do with wrapping should be the choice of the content consumer, not the responsibility of the content creator.

Are there any (legitimately) good reasons for adhering to 79 characters in this age?


回答 0

PEP-8的许多价值在于阻止人们参数无关紧要的格式化规则,并继续编写良好的,一致的格式化代码。当然,没有人真的认为79是最佳选择,但是将其更改为99或119或您首选的行长都没有明显的好处。我认为选择是这样的:遵循规则并找到值得争取的理由,或者提供一些数据来证明可读性和生产率如何随行长而变化。后者将非常有趣,并且我有很大的机会改变人们的想法。

Much of the value of PEP-8 is to stop people arguing about inconsequential formatting rules, and get on with writing good, consistently formatted code. Sure, no one really thinks that 79 is optimal, but there’s no obvious gain in changing it to 99 or 119 or whatever your preferred line length is. I think the choices are these: follow the rule and find a worthwhile cause to battle for, or provide some data that demonstrates how readability and productivity vary with line length. The latter would be extremely interesting, and would have a good chance of changing people’s minds I think.


回答 1

保持您的代码对人类可读,而不仅仅是机器可读。许多设备仍然一次只能显示80个字符。此外,它能够通过并排设置多个窗口,使屏幕较大的人更容易执行多项任务。

可读性也是强制行缩进的原因之一。

Keeping your code human readable not just machine readable. A lot of devices still can only show 80 characters at a time. Also it makes it easier for people with larger screens to multi-task by being able to set up multiple windows to be side by side.

Readability is also one of the reasons for enforced line indentation.


回答 2

我是一个程序员,每天必须处理大量代码。开源以及内部开发的东西。

作为一名程序员,我发现一次打开多个源文件很有用,并且经常在(宽屏)监视器上组织桌面,以便两个源文件并排。我可能两者都在编程,或者只是阅读其中一项,而另一种则在编程。

当这些源文件之一的宽度大于120个字符时,我会感到不满意和沮丧,因为这意味着我无法舒适地将一行代码放在屏幕上。它将格式设置换行。

我之所以说“ 120”,是因为这会导致我对超出范围的代码感到恼火。在输入了这么多字符之后,您应该为了便于阅读而将行分开,更不用说编码标准了。

我在编写代码时会考虑80列。只是这样,当我确实在该边界上泄漏时,这并不是一件坏事。

I am a programmer who has to deal with a lot of code on a daily basis. Open source and what has been developed in house.

As a programmer, I find it useful to have many source files open at once, and often organise my desktop on my (widescreen) monitor so that two source files are side by side. I might be programming in both, or just reading one and programming in the other.

I find it dissatisfying and frustrating when one of those source files is >120 characters in width, because it means I can’t comfortably fit a line of code on a line of screen. It upsets formatting to line wrap.

I say ‘120’ because that’s the level to which I would get annoyed at code being wider than. After that many characters, you should be splitting across lines for readability, let alone coding standards.

I write code with 80 columns in mind. This is just so that when I do leak over that boundary, it’s not such a bad thing.


回答 3

我相信那些研究版式的人会告诉您,每行66个字符应该是长度上最易读的宽度。即使这样,如果您需要通过ssh会话远程调试机器,大多数终端默认为80个字符,而79个恰好适合,在这种情况下尝试使用任何更宽的设备将是一个真正的痛苦。使用vim +屏幕作为日常环境的开发人员数量也将令您感到惊讶。

I believe those who study typography would tell you that 66 characters per a line is supposed to be the most readable width for length. Even so, if you need to debug a machine remotely over an ssh session, most terminals default to 80 characters, 79 just fits, trying to work with anything wider becomes a real pain in such a case. You would also be suprised by the number of developers using vim + screen as a day to day environment.


回答 4

在A4纸上,默认尺寸下的等宽字体打印为80列乘66行。

Printing a monospaced font at default sizes is (on A4 paper) 80 columns by 66 lines.


回答 5

这就是为什么我喜欢80个字符的原因:在工作中,我使用Vim,并在运行于1680×1040的监视器上一次处理两个文件(我不记得了)。如果行数不再长,即使使用自动换行,我也很难读取文件。不用说,我讨厌处理别人的代码,因为他们喜欢排长队。

Here’s why I like the 80-character with: at work I use Vim and work on two files at a time on a monitor running at, I think, 1680×1040 (I can never remember). If the lines are any longer, I have trouble reading the files, even when using word wrap. Needless to say, I hate dealing with other people’s code as they love long lines.


回答 6

由于空格在Python中具有语义含义,因此某些自动换行方法可能会产生不正确或模棱两可的结果,因此需要有一定的限制以避免这些情况。自从我们使用电传打字机以来,标准的行长为80个字符,因此79个字符似乎是一个非常安全的选择。

Since whitespace has semantic meaning in Python, some methods of word wrapping could produce incorrect or ambiguous results, so there needs to be some limit to avoid those situations. An 80 character line length has been standard since we were using teletypes, so 79 characters seems like a pretty safe choice.


回答 7

我同意贾斯汀。详细来说,人很难阅读过长的代码行,并且某些人的控制台宽度每行只能容纳80个字符。

推荐使用样式,以确保尽可能多的人在尽可能多的平台上尽可能舒适地阅读您编写的代码。

I agree with Justin. To elaborate, overly long lines of code are harder to read by humans and some people might have console widths that only accommodate 80 characters per line.

The style recommendation is there to ensure that the code you write can be read by as many people as possible on as many platforms as possible and as comfortably as possible.


回答 8

因为如果将其推到第80列之外,则意味着您正在编写的代码行很长且很复杂,执行了太多操作(因此您应该重构),或者缩进太多了(因此您应该重构)。

because if you push it beyond the 80th column it means that either you are writing a very long and complex line of code that does too much (and so you should refactor), or that you indented too much (and so you should refactor).


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。