推荐的长时间中断if语句的方法是什么? (W504在二进制运算符后换行)

时间:2019-07-17 10:58:22

标签: django python-3.x pep8 flake8

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

第一个选项

使用带有flake8的(which is from PEP8)以下样式,我收到警告:二进制运算符后W504换行:

if (this_is_one_thing and
    that_is_another_thing):
    do_something()

第二个选项

if (this_is_one_thing
    and that_is_another_thing):
    do_something()

现在我在二进制运算符之前收到警告W503换行。 第二个似乎与this recommendation from PEP8

一致

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

2 个答案:

答案 0 :(得分:0)

如果我们咨询documentation on flake 8,将会看到:

  

反模式

     

注意:尽管位于反模式部分中,   被认为是最佳做法。

income = (gross_wages
          + taxable_interest)
     

最佳做法

     

注意:尽管处于最佳做法部分,该很快就会成为   被认为是反模式

income = (gross_wages +
          taxable_interest)

因此将换行符之前视为最佳操作。

The documentation for W504,建议在新行之前建议操作员最佳实践,而无需给出注释:

  

反模式

income = (gross_wages +
          taxable_interest)
     

最佳做法

income = (gross_wages
          + taxable_interest)

答案 1 :(得分:0)

如有疑问,请询问Black

if (                                                           
    this_is_one_thing
    and that_is_another_thing
):                                                             
    do_something()                                             

很长一段时间以来,PEP-8建议在二进制运算符后的之后中断,但他们“最近”已切换为Donald-Knuth-approved break-before-binary-operator样式。