How to comment each condition in a multi-line if statement? How to comment each condition in a multi-line if statement? python python

How to comment each condition in a multi-line if statement?


Don't use \, use parenthesis:

if (CONDITION1 or    CONDITION2 or    CONDITION3):

and you can add comments at will:

if (CONDITION1 or  # condition1 is really cool    CONDITION2 or  # be careful of conditon2!    CONDITION3):   # see document A sec. B for info

Python allows for newlines in a parenthesised expression, and when using comments that newline is seen as being located just before the comment start, as far as the expression is concerned.

Demo:

>>> CONDITION1 = CONDITION2 = CONDITION3 = True>>> if (CONDITION1 or  # condition1 is really cool...     CONDITION2 or  # be careful of conditon2!...     CONDITION3):   # see document A sec. B for info...     print('Yeah!')... Yeah!