Python PEP 8文档字符串行长度

2024-05-14 23:19:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近开始想,跟着PEP 8走是个好主意。我让我的编辑器显示80列标记,现在正试图换行以适应它。我的问题是。在政治公众人物8中,它说:

Limit all lines to a maximum of 79 characters.

有道理。

[...] For flowing long blocks of text (docstrings or comments), limiting the length to 72 characters is recommended.

为什么是72岁?为什么79对docstring或comments不好?


Tags: ofto标记all编辑器comments政治pep
3条回答

docstring通常与函数缩进,并以三引号开头和结尾:

def foo(bar, baz):
    """Frobnicate the foobars into baz

    Parameters are ham and spammed.

    """

剩下79-4-3=72个字符。

我相信这是文本文件的宿醉:

At the end of the typewriter age, most designs were geared toward 72 CPL, derived from a pitch of 12 characters per inch, multiplied by 6 inches (see for example IBM Selectric). This would ensure at least 1 inch for each margin, with the U.S. government at the time having standardized on 8 1/2×11" paper.

Source

Many plain text documents still conform to 72 CPL out of tradition.

我认为这就像docstring经常在代码之外的上下文中使用一样,所以遵循大多数纯文本文档用来尽量保持一致的样式是有意义的。例如,man页的文本通常包装为72个字符。通过将docstring限制为72个字符,help()的输出反映了这一点。

This question on Programmers也可能是相关的。

行应该是79个字符的原因是,这样它们就可以容纳80个字符宽的终端(显然它们仍然存在)。在docstring上使用72个字符的原因是,这意味着两边的填充都相等(docstring毕竟是缩进的),并且仍然适合80个字符的宽度。

相关问题 更多 >

    热门问题