Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PEP8 - Contradiction between E129 and E127/E128

According to the PEP standards, indents should come before binary operators. Furthermore, multiline conditions should be enclosed within parentheses to avoid using backslashes before newlines. These two conventions lead to the following situation

if (long_condition_1
    or long_condition_2):
    do_some_function()

This code in turn breaks E129 visually indented line with same indent as next logical line in PEP8. However, the second line must be indented exactly four spaces, as otherwise it breaks E128 or E127 for under-indented or over-indented lines.

How should one format the above so that it confirms to PEP8 standards?

like image 321
Jon Claus Avatar asked Jun 19 '17 19:06

Jon Claus


2 Answers

This should work properly

if (long_condition_1 or
       long_condition_2):
    do_some_function()
like image 138
mdew Avatar answered Oct 25 '22 07:10

mdew


The answer to this question has changed over time. Due to a change in stance from PEP8, W503 is now widely regarded to go against PEP8.

PEP8 now says it's fine to break before OR after, but to keep it consistent locally.

For newer code, Knuth-style is preferred (which I think refers to breaking before the operator).

if (
    long_condition_1
    or long_condition_2
    or (
        long_condition_3
        and long_condition4
    )
):
    do_some_function()
like image 6
Ryan de Kleer Avatar answered Oct 25 '22 06:10

Ryan de Kleer