无法通过不合逻辑的线路pep8错误

时间:2014-03-20 01:21:38

标签: python if-statement pep8

我已经尝试解决这个问题一段时间了,我不能让它通过pep8。 这是我的代码:

1

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and 
    sum(regex.count(char) for char in splitter) == 1 and 
    regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')

2

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and 
    sum(regex.count(char) for char in splitter) == 1 and 
    regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')

3

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' 
    and regex.count('(') > 1):

    print('hi')

我在3个if语句中得到以下PEP8错误:

E125 continuation line does not distinguish itself from next logical line

知道它有什么问题吗?这些线条用第一个支架缩进,所以我真的没有线索。

3 个答案:

答案 0 :(得分:21)

1

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and
        sum(regex.count(char) for char in splitter) == 1 and
        regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')

2

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and
        sum(regex.count(char) for char in splitter) == 1 and
        regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')

3

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')'
        and regex.count('(') > 1):

    print('hi')

答案 1 :(得分:2)

我使用PyCharm(非常适合指出PEP8错误)进行编辑,它说这个版本没问题:

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and
        sum(regex.count(char) for char in splitter) == 1 and
        regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')

答案 2 :(得分:0)

我并不是说我喜欢这种解决方案,但我认为在if之后删除空格比将第二行与len调用的胆量排成一行不太妥协。其他答案在这里建议:

if(len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and
   sum(regex.count(char) for char in splitter) == 1 and
   regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')