Python 线太长。姜戈 PEP8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14143284/
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-18 10:36:00  来源:igfitidea点击:

Line is too long. Django PEP8

pythondjangopep8

提问by Angelina

PEP8 info:

PEP8 信息:

models.py:10:80: E501 line too long (83 > 79 characters)

Models.py:

模型.py:

field = TreeForeignKey('self', null=True, blank=True, related_name='abcdefgh')

How to correctly write this line?

如何正确地写这一行?

回答by Pavel Anossov

This is very subjective. I'd write, if I were following E501 strictly:

这是非常主观的。如果我严格遵循 E501,我会写:

field = TreeForeignKey('self',
                       null=True,
                       blank=True,
                       related_name='abcdefgh')

I usually consider 100 too long, not 80.

我通常认为 100 太长,而不是 80。

回答by mipadi

It's "correct", PEP8 just flags lines over 79 characters long. But if you're concerned about that, you could write it like this:

这是“正确的”,PEP8 只是标记长度超过 79 个字符的行。但是如果你担心这个,你可以这样写:

field = TreeForeignKey('self',
                       null=True,
                       blank=True,
                       related_name='abcdefgh')

Or this:

或这个:

field = TreeForeignKey(
    'self',
    null=True,
    blank=True,
    related_name='abcdefgh',
)

Or, really, any other style that would break the single line into multiple shorter lines.

或者,实际上,任何其他样式都可以将单行分成多条较短的行。

回答by umeboshi

I just found this neat program called autopep8! https://github.com/hhatto/autopep8

我刚刚找到了这个名为 autopep8 的简洁程序!https://github.com/hhatto/autopep8

pip install autopep8
autopep8 -i models.py

You can also do (recursively):

您还可以(递归地)执行以下操作:

autopep8 -ri package/

Auto PEP8 only makes safe changes to the files, only changing layout, not code logic.

Auto PEP8 只对文件进行安全更改,只更改布局,而不是代码逻辑。

回答by Kirk Strauser

I usually split that to line up the parameters one level of indentation deeper than the original line, like:

我通常将其拆分以排列比原始行更深一层缩进的参数,例如:

field = TreeForeignKey('self', null=True,
    blank=True, related_name='abcdefgh')

Especially if TreeForeignKeyis something like TreeForeignKeyWithReferencesToSomethingElse, in which case all the parameters would start out to the far right of the window if you lined them all up with the opening parenthesis. If any of the parameters had a long name like defaultvalueforcertaincircumstances, you might not be able to fit the whole thing in under 80 columns:

特别是如果TreeForeignKey是这样的TreeForeignKeyWithReferencesToSomethingElse,在这种情况下,如果您将它们全部与左括号对齐,则所有参数都将从窗口的最右侧开始。如果任何参数的名称很长,例如defaultvalueforcertaincircumstances,您可能无法将整个内容放入 80 列以下:

field = TreeForeignKeyWithReferencesToSomethingElse('self',
                                                    defaultvalueforcertaincircumstances='foo')

I also prefer to put multiple function parameters on the same line (except when it just doesn't look right; I'm not a purist!) so that vertical space isn't overly expanded, causing me to spend more time scrolling around in my editor than otherwise necessary.

我也更喜欢将多个函数参数放在同一行上(除非它看起来不正确;我不是纯粹主义者!)这样垂直空间就不会过度扩展,导致我花更多时间在里面滚动我的编辑比其他必要的。

回答by árni St. Siguresson

If you have some ridiculous long string that isn't very convenient to break into pieces (thinking about things like Sentry DSNs, the occasional module in MIDDLEWARE or INSTALLED_APPS), you can just put # noqaat the end of the line and the linters will ignore the line. Use sparingly thou and definitely not for the case you asked for.

如果您有一些不方便分解的荒谬长字符串(考虑诸如 Sentry DSN、MIDDLEWARE 或 INSTALLED_APPS 中的偶然模块),您可以将其放在# noqa行尾,linter 将忽略线。请谨慎使用,绝对不要用于您要求的情况。

回答by Harshal Pandit

autopep8can solve spacing issues in files and entire project as well, here is a link to the documentation: https://github.com/hhatto/autopep8

autopep8也可以解决文件和整个项目中的间距问题,这里是文档的链接:https: //github.com/hhatto/autopep8

  1. pip install --upgrade autopep8
  2. autopep8 --in-place --aggressive --aggressive <path_with_python_filename>
    ex: autopep8 --in-place --aggressive --aggressive C://a/test.py
  1. pip install --upgrade autopep8
  2. autopep8 --in-place --aggressive --aggressive <path_with_python_filename>
    前任: autopep8 --in-place --aggressive --aggressive C://a/test.py