Fix Python – Very Long If Statement in Python [duplicate]


Asked By – rectangletangle

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.

Now we will see solution for issue: Very Long If Statement in Python [duplicate]


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

This question is answered By – dappawit

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0