I am delighted to see that PEP-8 has pivoted to breaking long formulae before, rather than after, each binary operator! But I would like to pivot the PEP away from citing my own PyCon Canada talk as the authority on the matter, and toward citing Knuth himself. It would also be an enhancement for the PEP to show both options and make an argument for the practice, instead of simply asserting that one is better than the other. I therefore propose the attached patch.
Another important objection against the current text is that it stacks a series of `and` and `or` operators at the same level of indentation, as though they naturally evaluate in the order the programmer writes them. In fact, they have different levels of precedence, and the code example violates the other sections of PEP-8 that ask for the creation of a visual distinction in code between different precedence levels. The example needs to pivot towards a series of operators which belong at the same precedence level. I have used `+` and `-` because they seemed more natural to form an example from than something like division and multiplication.
Patch LGTM. Note that the and/or example was in the PEP before this discussion broke out. The discussion also at some point (before your Knuth quote was discovered) veered in the direction of making and/or a special case, perhaps because it's more likely that a long expression must be broken around and/or operators. And there were already examples of breaking after binary arithmetic operators elsewhere in the PEP (I think).
The patch is committed in changeset 9afe77ad549b. Thanks for update Brandon. Citing the original source and stating the rationale for this suggestion was a great addition.
History
Date
User
Action
Args
2022-04-11 14:58:29
admin
set
github: 70967
2016-04-20 06:17:58
orsenthil
set
status: open -> closednosy: + orsenthilmessages: + resolution: fixedstage: resolved