157

I have a very long if statement in Python. What is the best way to break it up into several lines? By best I mean most readable/common.

rectangletangle
  • 50,393
  • 94
  • 205
  • 275

2 Answers2

253

According to PEP8, long lines should be placed in parentheses. When using parentheses, the lines can be broken up without using backslashes. You should also try to put the line break after boolean operators.

Further to this, if you're using a code style check such as pycodestyle, the next logical line needs to have different indentation to your code block.

For example:

if (abcdefghijklmnopqrstuvwxyz > some_other_long_identifier and
        here_is_another_long_identifier != and_finally_another_long_name):
    # ... your code here ...
    pass
Thiago Figueiro
  • 410
  • 5
  • 15
dappawit
  • 12,182
  • 2
  • 32
  • 26
  • Make sure to turn off `E129` or use Andrew Clark's solution (http://stackoverflow.com/a/5253419/981933) otherwise PEP8 will throw an error at you. See: https://github.com/PyCQA/pep8/issues/126 and https://github.com/PyCQA/pep8/issues/386 – F Lekschas Dec 09 '15 at 16:57
  • Don't turn off `E129`, just comply with it. It is hard to read things without it. – Błażej Michalik Sep 15 '16 at 12:42
  • Well, I don't know how to comply with `E129`. It's not happy with anything I try. – user2061057 Mar 29 '17 at 11:21
  • 6
    To comply with E129 add 4 more spaces before here_is_another_long_identifier [..] The example below by Andrew Clark explains best and should be the real answer. – Michael van de Waeter May 01 '17 at 12:44
  • 2
    Just a comment on your solution the break should be before the 'and' operator. the new rule of pep8. – Arb Jul 30 '21 at 09:23
  • As it states on the PEP 8 page: "For new code Knuth’s style is suggested." which is the style with the line break before operators. It is not a 'rule' though. – Firefighting Physicist Nov 24 '22 at 08:41
53

Here is the example directly from PEP 8 on limiting line length:

class Rectangle(Blob):

    def __init__(self, width, height,
                 color='black', emphasis=None, highlight=0):
        if (width == 0 and height == 0 and
                color == 'red' and emphasis == 'strong' or
                highlight > 100):
            raise ValueError("sorry, you lose")
        if width == 0 and height == 0 and (color == 'red' or
                                           emphasis is None):
            raise ValueError("I don't think so -- values are %s, %s" %
                             (width, height))
        Blob.__init__(self, width, height,
                      color, emphasis, highlight)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • 4
    But this causes E129, visually indented line with the same indent as next logical line, using the pep8 lint checker. – ArtOfWarfare Aug 14 '14 at 14:26
  • @ArtOfWarfare The indentation got messed up. Correction pending review or look directly at the example in [PEP8](http://legacy.python.org/dev/peps/pep-0008/). – altendky Sep 08 '14 at 18:31
  • 2
    Readability is damaged by putting multiple parameters on one line, but then still breaking and putting the rest on a second line. Either break after each or keep them all in one line, if it is not too long. "What's so special about `height`, that there is a break after it?" "Why not break after `width` or `color`?" These kind of questions only arise with breaking, which does not follow a strict rule. – Zelphir Kaltstahl Sep 28 '17 at 10:28
  • it's still compliant if you break according to E129 and each line stays within 80 columns, which what the linter checks for. Note the non-associative operators. – cowbert Mar 23 '18 at 23:19