Pep8和多线同时条件

时间:2017-07-24 17:05:16

标签: python indentation multiline pep8 continuation

为了避免pep8的抱怨,我今天不得不在一段时间内添加无关的parens:

while not found and not something and \
    (time_left is None or time_left > 0):
    (one, two, three, four) = self.gimme(timeout=time_left)

我的解决方案:

while (not found and not something and
       (time_left is None or time_left > 0)):
    (one, two, three, four) = self.gimme(timeout=time_left)

如果我改变了第二行的缩进,它就会抱怨过度缩进或缺少缩进,即使每次缩进都是W,而右边是8。

我很担心添加外来的parens来满足pep8,因为可读性的提高很少,这违背了一般原则。

有什么想法吗?我错过了更好的解决方案吗?

2 个答案:

答案 0 :(得分:4)

我更喜欢在条件语句之后打破长行以提高可读性。 e.g:

while (
    not found and 
    not something and 
    (time_left is None or time_left > 9)
):
    (one, two, three, four) = self.gimme(timeout=time_left)

我认为这是非常易读的,至少可以满足我的pep8代码检查。

答案 1 :(得分:0)

我认为最好的解决方案是做任何你(以及你的团队,如果适用)认为是最易读的解决方案。 PEP8只是一个指导原则,不是必需的。专注于编写强大且易于理解的代码。