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

Guido van Rossum guido at python.org
Fri Apr 15 12:53:13 EDT 2016

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.

The head of the python-ideas discussion:

See also the discussion in the tracker: http://bugs.python.org/issue26763

Here's the diff I applied: https://hg.python.org/peps/rev/3857909d7956

The talk by Brandon Rhodes where Knuth is referenced ([3] below):

The key section in PEP 8 that was updated (apart from fixing up references):

Should a line break before or after a binary operator?

For decades the recommended style has been to break after binary
operators.  However, recent reseach unearthed recommendations by
Donald Knuth to break *before* binary operators, in his writings about
typesetting [3]_.  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)

--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20160415/84b8d98d/attachment.html>

More information about the Python-Dev mailing list