[Python-Dev] PEP 8 updated on whether to break before or after a binary update

Tim Peters tim.peters at gmail.com
Fri Apr 15 13:02:43 EDT 2016


[Guido]
> After a fruitful discussion on python-ideas I've decided that it's fine to
> break lines *before* a binary operator. It looks better and Knuth recommends
> it.
> ...
> Therefore it is permissible to break before or
> after a binary operator, as long as the convention is consistent
> locally.  For new code Knuth's style is suggested.
>
> Some examples of code breaking before binary Boolean operators::
>
>     class Rectangle(Blob):
>
>         def __init__(self, width, height,
>                      color='black', emphasis=None, highlight=0):
>             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))
>             Blob.__init__(self, width, height,
>                           color, emphasis, highlight)
>

Note that this code still breaks a line after a binary operator (the
string formatting "%" operator in the 2nd ValueError call).  But it's
perfectly clear the way it is.  Good taste can't be reduced to rules
;-)


More information about the Python-Dev mailing list