打破长if语句的推荐方法是什么?(二元运算符后的 W504 换行符)

ann*_*piv 25 python pep8 python-3.x flake8

目前推荐的用“and”和“or”运算符打破一长串if语句的推荐方法是什么?

第一个选项

使用下面的样式(来自 PEP8)和 flake8 我收到警告:二元运算符后的 W504 换行符:

if (this_is_one_thing and
    that_is_another_thing):
    do_something()
Run Code Online (Sandbox Code Playgroud)

第二个选项

if (this_is_one_thing
    and that_is_another_thing):
    do_something()
Run Code Online (Sandbox Code Playgroud)

现在我在二元运算符之前收到警告 W503 换行符。第二个似乎符合PEP8 的这个建议

我试图找到答案,但我仍然不确定。我认为也许使用第二个选项并禁用 W503 警告将是处理此问题的一种方法?

Wil*_*sem 21

如果我们查阅有关flake 8文档,我们会看到:

反模式

注意:尽管在反模式部分,这很快就会被认为是最佳实践。

income = (gross_wages
          + taxable_interest)
Run Code Online (Sandbox Code Playgroud)

最佳实践

注意:尽管在最佳实践部分,这很快将被视为反模式

income = (gross_wages +
          taxable_interest)
Run Code Online (Sandbox Code Playgroud)

因此,在二元运算符之前的换行符将被视为最佳实践。

W504的文档在新行之前建议操作员作为最佳实践,没有给定的注释:

反模式

income = (gross_wages +
          taxable_interest)
Run Code Online (Sandbox Code Playgroud)

最佳实践

income = (gross_wages
          + taxable_interest)
Run Code Online (Sandbox Code Playgroud)


L3v*_*han 10

如有疑问,请询问布莱克

if (                                                           
    this_is_one_thing
    and that_is_another_thing
):                                                             
    do_something()                                             
Run Code Online (Sandbox Code Playgroud)

很长一段时间,PEP-8 建议二元运算符之后中断,但他们“最近”切换到了Donald-Knuth 批准的二元运算符之前的中断样式。