Very Long If Statement in Python

Very Long If Statement in Python

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 youre 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 ...

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 dont think so -- values are %s, %s %
                             (width, height))
        Blob.__init__(self, width, height,
                      color, emphasis, highlight)

Very Long If Statement in Python

Leave a Reply

Your email address will not be published. Required fields are marked *