Python 为什么 PyCharm 使用 120 个字符行,即使 PEP8 指定了 79 个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26808681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:02:23  来源:igfitidea点击:

Why does PyCharm use 120 Character Lines even though PEP8 Specifies 79?

pythonpycharmpep8

提问by Samuel

PEP8 clearly specifies 79 characters, however, PyCharm defaults to 120 and gives me the warning "PEP8: line too long (... > 120 characters)".

PEP8 明确指定了 79 个字符,但是,PyCharm 默认为 120 个并给我警告“PEP8:行太长(...> 120 个字符)”。

Did previous versions of PEP8 use 120 and PyCharm not update its PEP8 checker? I couldn't find any previous versions of the PEP8 Guide, however, I can easily find previous version of the PEP8 Python scripts.

以前版本的 PEP8 使用 120 并且 PyCharm 没有更新其 PEP8 检查器吗?我找不到 PEP8 指南的任何先前版本,但是,我可以轻松找到 PEP8 Python 脚本的先前版本。

I'm starting a new Python project and I'm not sure which to use.

我正在开始一个新的 Python 项目,但我不确定要使用哪个。

References:

参考:

http://legacy.python.org/dev/peps/pep-0008/

http://legacy.python.org/dev/peps/pep-0008/

采纳答案by munk

PyCharm is built on top of IntelliJ. IntelliJ has a default line length of 120 characters.

PyCharm 建立在 IntelliJ 之上。IntelliJ 的默认行长为 120 个字符。

This is probably because you can't fit a common Java name like: @annotated public static MyObjectFactoryFactory enterpriseObjectFactoryFactoryBuilderPattern {in a mere 80 character line. (I'm poking fun, but Java names do tend to be longer by convention).

这可能是因为您无法@annotated public static MyObjectFactoryFactory enterpriseObjectFactoryFactoryBuilderPattern {在仅 80 个字符的行中放入一个常见的 Java 名称,例如:。(我在开玩笑,但按照惯例,Java 名称确实会更长一些)。

The pep8 checker is configurable, so you can specify a better max line length - like 79 characters.

pep8 检查器是可配置的,因此您可以指定更好的最大行长度 - 例如 79 个字符。

The error is misleading because the pep8 checker formats the text with something like "PEP8: line too long(... > %s characters)" % max_line_setting. So it's using the pep8 checker, with a specific configuration, not claiming that pep8 specifies a 120 character line.

该错误具有误导性,因为 pep8 检查器使用类似"PEP8: line too long(... > %s characters)" % max_line_setting. 因此,它使用具有特定配置的 pep8 检查器,而不是声称 pep8 指定了 120 个字符的行。

回答by Jon Kiparsky

AFAIK, PEP8 has never allowed 120 characters, but not everyone follows PEP8. To answer your question: stay under 80 characters, both from common courtesy and good sense.

AFAIK,PEP8 从来没有允许 120 个字符,但并不是每个人都遵循 PEP8。回答你的问题:保持在 80 个字符以下,既是出于礼貌,也是出于好意。

回答by Austin Heller

If you want to remove the limit warning altogether you can take the following steps:

如果要完全删除限制警告,可以执行以下步骤:

  1. In PyCharm, click File > Settings
  2. In the project settings section, click Editor > Inspections
  3. In the list that appears, expand Python
  4. Under Python, scroll down and click "PEP8 coding style violation"
  5. Click the + button next to "Ignore errors" in the bottom right
  6. Type out E501 and click Apply and/or OK
  1. 在 PyCharm 中,单击文件 > 设置
  2. 在项目设置部分,单击编辑器 > 检查
  3. 在出现的列表中,展开 Python
  4. 在 Python 下,向下滚动并单击“PEP8 编码风格违规”
  5. 单击右下角“忽略错误”旁边的 + 按钮
  6. 输入 E501 并单击应用和/或确定

Sources:

资料来源: