python - Python 的 PEP8 行长度限制是否适用于注释?

标签 python pep8

<分区>

我正在从事一个协作式 Python 项目。由于 PEP8,我的 IDE (PyCharm) 将代码行的长度超过 120 个字符标记为“小问题”。然而,只有当我有一个大的注释来解释一些复杂的代码时才会发生这种情况——代码本身永远不会超过限制。我应该将这些长评论变成多行评论以遵守指南还是保持原样?

最佳答案

是的,指南适用于源代码中的所有行,包括注释。那是因为它们有助于设定对编辑器宽度的期望。如果行长度建议不适用于评论,您仍然必须滚动编辑器窗 Eloquent 能阅读评论。

PEP8还是一个指南,不是严格的法治,可以随意忽略。然而,我建议在任何与更广泛的人合作的地方坚持使用它,即使你们都有更宽的屏幕。例如,就个人而言,我喜欢能够并排放置多个编辑器选项卡。

在 Facebook,我们使用 flake8-bugbear - 强制行长度,容差为 10%:

B950: Line too long. This is a pragmatic equivalent of pycodestyle’s E501: it considers “max-line-length” but only triggers when the value has been exceeded by more than 10%. You will no longer be forced to reformat code due to the closing parenthesis being one character too far to satisfy the linter. At the same time, if you do significantly violate the line length, you will receive a message that states what the actual limit is. This is inspired by Raymond Hettinger’s “Beyond PEP 8” talk and highway patrol not stopping you if you drive < 5mph too fast. Disable E501 to avoid duplicate warnings.

义务免责声明:我为 flake8-bugbear 做出了贡献。

关于python - Python 的 PEP8 行长度限制是否适用于注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46863890/

相关文章:

python - 正则表达式捕获 html 元素及其类名

python - 将子字符串的第一个实例与正则表达式匹配

python - `Ctrl` + `Alt` + `L` 不修复警告 PEP 8 : E122 continuation line missing indentation or outdented

python - 我应该将 Pandas DataFrame 列名定义为字符串变量吗?

python - 在抽象类中使用children方法可以吗? pep8 说实例没有成员

python - Python 2.7 的 Jupyter 安装失败

python - python中具有平方逆权重的Knn分类器(权重是一个可调用函数)

Python (Tkinter) 如何只显示Mysql记录而不显示列表?

Python 核心库和 PEP8

python - autopep8 与 vim