Can't get past illogical line pep8 error

南楼画角 提交于 2019-12-05 20:32:20

问题


I've been trying to fix this for a while now and i just can't get it to pass pep8. Here is my code:

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')

I get the following PEP8 error on each of the 3 if statements:

E125 continuation line does not distinguish itself from next logical line

Any idea on what's wrong with it? The lines are indented with the first bracket so i really don't have a clue.


回答1:


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')



回答2:


I'm using PyCharm (which is pretty good for pointing out PEP8 errors) for my editing, and it says this version is ok:

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:


I'm not saying I love this solution, but I think that removing the space after if is less of a compromise than lining up the second line with the guts of the len call, like the other answers here suggest:

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')


来源:https://stackoverflow.com/questions/22521608/cant-get-past-illogical-line-pep8-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!