PEP 7: Break lines before operators, like PEP 8?

In 2016, PEP 8 was updated to prefer breaking lines before operators for new code. The section has rationale.

PEP 7 still recommends the older style:

When you break a long expression at a binary operator, the operator goes at the end of the previous line, and braces should be formatted as shown. E.g.:

if (type->tp_dictoffset != 0 && base->tp_dictoffset == 0 &&
    type->tp_dictoffset == b_size &&
    (size_t)t_size == b_size + sizeof(PyObject *))
{
    return 0; /* "Forgive" adding a __dict__ only */
}

Is it time to update the C style too?

11 Likes

Yes, this is more readable to me:

if (type->tp_dictoffset != 0 
    && base->tp_dictoffset == 0
    && type->tp_dictoffset == b_size
    && (size_t)t_size == b_size + sizeof(PyObject *))
{
    return 0; /* "Forgive" adding a __dict__ only */
}
6 Likes

Yes, please. I’m already violating this PEP 7 rule since I prefer the PEP 8 style: IMO it’s more readable to start a line with the operator.

PEP 7 should just say that old code uses legacy formatting, but new code should follow the latest PEP 7 formatting rules.

4 Likes

Here’s a PR: PEP 7: Break lines before operators ("Knuth's style") by encukou · Pull Request #3931 · python/peps · GitHub

[Edit] It’s now merged.