11

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')
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')
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 parenthesis so i really don't have any clue.

Bip Lob
  • 485
  • 2
  • 6
  • 15
user3050527
  • 881
  • 1
  • 8
  • 15
  • What exactly is the problem? – thefourtheye Mar 20 '14 at 01:23
  • pep8 gives me the following error: regex_functions_draft.py:44:9: E125 continuation line does not distinguish itself from next logical line. I tried indenting it correctly with the first bracket but it doesn't seem to work. – user3050527 Mar 20 '14 at 01:24
  • 3
    @FredMitchell regex is just a variable and isn't relevant. The if statement works, it's just how pep8 handled how i indented the if statement. – user3050527 Mar 20 '14 at 01:41
  • Related: http://stackoverflow.com/questions/181530/python-style-multiple-line-conditions-in-ifs – Blckknght Mar 20 '14 at 02:00
  • There's also an interesting bug report about this issue on the PEP8 module's github page: https://github.com/jcrocholl/pep8/issues/126 – Blckknght Mar 20 '14 at 02:01

3 Answers3

28

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')
user3412839
  • 576
  • 3
  • 9
  • 2
    As a newbie to python, I had scrolled up and down 3 times until I noticed the indention difference. :) – lfree Jul 17 '15 at 03:43
  • [There](https://github.com/PyCQA/pep8/issues/126) has a brief description and I quote here: Here is the relevant statement from pep8: Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent. When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line. – lfree Jul 17 '15 at 03:43
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')
Steinar Lima
  • 7,644
  • 2
  • 39
  • 40
  • 4
    This seems awkward to me, since the `sum` and `regex.count` lines are not aligned with the parenthesis that they're nested in. It looks like they're part of the `len` call, while they're outside of it. – Blckknght Mar 20 '14 at 01:42
-1

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')
Jim Hunziker
  • 14,111
  • 8
  • 58
  • 64