英文:
How can I split a for statement over multiple lines with Flake8?
问题
我目前正在尝试编写一个超过80个字符的for循环语句。
```py
for i, (expected, actual) in enumerate(zip_longest(self.__lines, lines)):
...
# 请注意,上面的行长为73个字符。将其放在类和方法中后,它变为81个字符长。
# 为了简化问题,我没有包括类定义,但我需要添加这个注释来解释,因为有人抱怨该行实际上不是> 80个字符长。
为了满足Flake8中的最大行长度规则,我尝试将其拆分成多行,但我找到的任何组合似乎都不能满足它。
for i, (expected, actual)\
in enumerate(zip_longest(self.__lines, lines)):
# ^ 与下一个逻辑行相同缩进的连续行 - flake8(E125)
...
for i, (expected, actual)\
in enumerate(zip_longest(self.__lines, lines)):
# ^ 缺少缩进或缩进错误的连续行 - flake8(E122)
...
如何将我的for循环拆分成多行,而不需要在配置中禁用这些规则或添加# noqa
注释?
<details>
<summary>英文:</summary>
I am currently trying to write a for statement that is over 80 characters long.
```py
for i, (expected, actual) in enumerate(zip_longest(self.__lines, lines)):
...
# Note that the line above is 73 characters long. It becomes 81
# characters long once you put it in a class and a method.
# I did not include the class definition for the sake of the
# simplicity of the question, but I've needed to add this comment
# explaining this because people have complained that the line isn't
# actually > 80 characters long.
In order to satisfy the maximum line length rule in Flake8, I have attempted to split it into multiple lines, but no combination I've found appears to satisfy it.
for i, (expected, actual)\
in enumerate(zip_longest(self.__lines, lines)):
# ^ continuation line with same indent as next logical line - flake8(E125)
...
for i, (expected, actual)\
in enumerate(zip_longest(self.__lines, lines)):
# ^ continuation line missing indentation or outdented - flake8(E122)
...
How can I split my for loop over multiple lines without needing to disable these rules in my config or add a # noqa
comment?
答案1
得分: 2
black建议您将其翻译为以下方式:
for i, (expected, longish_actual) in enumerate(
zip_longest(self.__lines, lines)
):
特别是,pep-8 提供了以下建议:
包装长行的首选方法是使用Python中括号、方括号和大括号内隐含的行继续。长行可以通过将表达式包装在括号中来分成多行。这应该优先于使用反斜杠进行行继续。
所以,想象一下,不是使用 enumerate
,而是连接了多个列表 a + b + c + d
,可能使用的是比单个字符更长的名称。按照上面的建议,可以这样写:
for x in (a + b
+ c + d):
也就是说,即使连接操作不一定需要括号,也应该优先使用"额外"的括号来包装表达式,以便下一行能够被正确解析。
英文:
black
suggests you phrase it like this:
for i, (expected, longish_actual) in enumerate(
zip_longest(self.__lines, lines)
):
In particular,
pep-8
offers this advice:
> The preferred way of wrapping long lines is by using Python’s implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
So imagine that instead of the enumerate
we had
catenation of several lists a + b + c + d
,
perhaps with longer than single-character names.
The above advice would be followed in this way:
for x in (a + b
+ c + d):
That is, even though the catenation doesn't need (
)
parentheses, prefer to wrap the expression with "extra" parens
so the next line will be properly parsed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论