Continuing indentation

Tim Chase python.list at tim.thechases.com
Wed Mar 2 18:00:41 EST 2016


On 2016-03-03 08:29, Ben Finney wrote:
> Skip Montanaro <skip.montanaro at gmail.com> writes:
>> Running flake8 over some code which has if statements with
>> multiple conditions like this:
>>
>>     if (some_condition and
>>         some_other_condition and
>>         some_final_condition):
>>         play_bingo()
> 
> For this reason I prefer to indent all continuation lines 8 spaces::
> 
>     if (
>             some_condition and
>             some_other_condition and
>             some_final_condition):
>         play_bingo()

This is generally what I do with two modifications, (1) putting
the conjunction at the beginning makes it easier for me to
read, and (2) putting the close-paren and colon on their own line to
make diffs cleaner when adding/removing conditions:

    if (
            some_condition
            and some_other_condition
            and some_additional_condition
            ):
        play_bingo()

making my diffs look like

     if (
             some_condition
             and some_other_condition
             and some_additional_condition
+            and some_new_condition
             ):
         play_bingo()

instead of

     if (
             some_condition
             and some_other_condition
-            and some_additional_condition):
+            and some_additional_condition
+            and some_new_condition):
         play_bingo()

which is harder to follow, IMHO.

Though, as a side note, if I have lots of "and" or "or" conjunctions,
I tend to use any() or all() on a list of them:

     if all([
             some_condition,
             some_other_condition,
             some_additional_condition,
+            some_new_condition,
             ]):
         play_bingo()

which I happen to find even tidier (though it might come at a minor
performance penalty, having not tested it since it's never mattered
in my code)

-tkc







More information about the Python-list mailing list