gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:06:46 25 4
gpt4 key购买 nike

目前推荐的用“and”和“or”运算符打破一长串 if 语句的推荐方法是什么?
第一个选项
以下款式(which is from PEP8)使用 flake8 我收到警告:二元运算符后的 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 警告将是解决此问题的一种方法?

最佳答案

如果我们咨询documentation on flake 8我们看:

Anti-pattern

Note: Despite being in the anti-pattern section, this will soon be considered the best practice.

income = (gross_wages
+ taxable_interest)

Best practice

Note: Despite being in the best practice section, this will soon be considered an anti-pattern.

income = (gross_wages +
taxable_interest)


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

The documentation for W504 , 在新行之前建议运算符(operator)作为最佳实践,没有给定的注释:

Anti-pattern

income = (gross_wages +
taxable_interest)

Best practice

income = (gross_wages
+ taxable_interest)

关于python - 打破长if语句的推荐方法是什么? (二元运算符后的 W504 换行符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57074300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com