我正在学习Python中的样式,在阅读PEP 8时,我对以下内容感到困惑。根据PEP 8,如果你使用悬挂缩进,你应该添加更多缩进以区分参数与函数体:
# More indentation included to distinguish this from the rest.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
然后我找到了这段代码,并想问你是否可以在这种情况下,条件与其余条件处于相同的缩进级别。这应该被视为悬挂缩进吗?或者由于它是垂直对齐的,可以吗?
if (value1 == 0 and value2 == 0 and
value3 == 'valueX' and value4 == 'valueY' or
value5 > value6):
raise ValueError("test")
提前致谢!
答案 0 :(得分:3)
在http://pep8online.com/上,您可以查看符合PEP8规范的代码段。
如果您粘贴此
if (value1 == 0 and value2 == 0 and
value3 == 'valueX' and value4 == 'valueY' or
value5 > value6):
raise ValueError("test")
您会看到它提供以下内容
E125 line 3 column 5: continuation line does not distinguish itself from next logical line
进一步缩进第3行(4个空格)会使错误消失。下面的代码是有效的,无论它看起来多么奇怪。
if (value1 == 0 and value2 == 0 and
value3 == 'valueX' and value4 == 'valueY' or
value5 > value6):
raise ValueError("test")
它可能更像是可读的,这是另一个有效的解决方案:
if (value1 == 0 and value2 == 0 and
value3 == 'valueX' and value4 == 'valueY' or
value5 > value6):
raise ValueError("test")
答案 1 :(得分:1)
阅读以下PEP-8计数器示例的评论:
# Further indentation required as indentation is not distinguishable
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
直接得出结论,问题是区分缩进。因此,我想说你的例子需要进一步缩进。