4 ms·
From PEP 8: > The preferred place to break around a binary operator is after the operator, not before it. I'd be interested in hearing the justification for t
by BrandonM 16y ago
From PEP 8:
> The preferred place to break around a binary operator is after the operator, not before it.
I'd be interested in hearing the justification for this rule. I think that leading a continuation line with the binary operator makes it super-clear that it is a continuation line. What is the benefit of the preferred style? Compare:
if (the_result_of_this_function(on_this_arg) == 10
and this_overly_descriptive_boolean):
do_stuff()
if (the_result_of_this_function(on_this_arg) == 10 and
this_overly_descriptive_boolean):
do_stuff()
To me, the first one is quite clearly a continuation line (no statement can start with "and"). The second requires closer inspection.
- ogrisel 16y agoI agree on this specific case but consistency with conventions shared across projects is more important. But I don't think anybody will complain if you use either of the them whereas 160 chars long expressions with no spacing between operators and funkyCamelCasing all over the code are just show-stoppers when I want to contribute a patch to a project.
- zephyrfalcon 16y agoI would write: if the_result_of_this_function(on_this_arg) == 10 \ and this_overly_descriptive_boolean: do_stuff() Indenting the second line of the if statement would, at first glance, indicate that it's part of the block instead. Then again, it depends. If it was the header of a def statement, I would follow the PEP, e.g. def __init__(self, width, height, color='black', emphasis=None, highlight=0): On a side note, I once did the "Art & Logic challenge" [http://www.artlogic.com/ http://www.artlogic.com/] and they use guidelines that apply to several languages, e.g. you would use the same formatting style for C++ and for Python, if at all possible. Much of it flies in the face of PEP 8.