When indenting long if conditions, you usually do something like this (actually, PyDev indents like that):
if (collResv.repeatability is None or collResv.somethingElse): collResv.rejected = True collResv.rejectCompletely()
However, this puts the block started by the if statement on the same indentation level as the last part of the if condition which makes it very ugly/hard to read in my opinion as you don't immediately see where the block starts.
Some other styles I thought about:
if (collResv.repeatability is None or collResv.somethingElse): collResv.rejected = True collResv.rejectCompletely()
This looks pretty inconsistent as the second line is indented much more than the first line but it's readable.
if (collResv.repeatability is None or collResv.somethingElse): collResv.rejected = True collResv.rejectCompletely()
This is also more readable than the first example, but the indentation is not a multiple of 4 anymore and besides that it looks wrong as the second line has less indentation than the beginning of the condition in the first line.
So, my main question is: Is there a suggested indentation style for cases like that which do not require overly-long lines (i.e. a single-line condition)? If not, what do you prefer for cases like that?
After the if-statement, a section of code to run when the condition is True is included. The section of <code to run> must be indented and every line in this section of code must be indented the same number of spaces. By convention, four space indentation is used in Python.
In Python, I prefer to use vertical space, enclose parenthesis, and place the logical operators at the beginning of each line so the expressions don't look like "floating". If the conditions need to be evaluated more than once, as in a while loop, then using a local function is best.
Long if statements may be split onto several lines when the character/line limit would be exceeded. The conditions have to be positioned onto the following line, and indented 4 characters. The logical operators ( && , || , etc.)
If the if statement is indented with spaces, use the same number of spaces for else . If the if statement is indented with one or more tabs, use the same number of tabs for the else statement. Don't mix spaces and tabs for indentation.
Often I work around this problem by calculating the condition in an own statement:
condition = (collResv.repeatability is None or collResv.somethingElse) if condition: collResv.rejected = True collResv.rejectCompletely()
Though, for a still relatively short condition as in your specific example I'd go for nosklo's solution - the extra statement used here is more suited for even longer conditional expressions.
This is what I do:
if (collResv.repeatability is None or collResv.somethingElse): collResv.rejected = True collResv.rejectCompletely()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With