I have a looong statement of "and"'s, for example:
if this == that and this == that and this == that:
do this
How to correctly break such statements into separate lines, to comply with PEP-8?
I have a looong statement of "and"'s, for example:
if this == that and this == that and this == that:
do this
How to correctly break such statements into separate lines, to comply with PEP-8?
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.
and examples:
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))
You can separate lines in Python, but mark them to be the same "semantic" line, by inserting backslashes:
if this == that \
and this == that \
and this == that:
do this
This will do even if you have and
's or or
's in your complex conditional expression.
Hope this helps.