WIP: Use assign expr for "while True: ... break" by vstinner · Pull Request #8095 · python/cpython (original) (raw)
The PEP explicitly rejected requiring parentheses, giving as one concrete example that they'd "look redundant" in:
# Top level in if
if match := pattern.match(line):
return match.group(1)
Which is exactly the kind of case in which you're always adding them and are getting feedback that it just doesn't look right to experienced Python eyes. I didn't write that part or the PEP, or even participate in the discussion that resulted in it being added - I just find it to be obviously true 😉
I can't think of any case in which anyone recommends adding redundant parentheses except when clarifying evaluation order.
For example, I doubt anyone would object to the redundant parens in:
or
But I can't think either of any experienced Python programmer who does champion the redundant parens in
i = (1 + 2)
i = (((1) + (2)))
return (1 + 2)
if (x or y):
while (x > y):
...
etc. The parens don't clarify anything in those - they just add counterproductive line noise. Newcomers from other languages where parens are required in such contexts often start off adding them anyway, and that's fine, but most grow out of it soon enough even if they're not slapped for it in a code review 😉
You started off by saying you wanted a discussion of coding guidelines for use of :=
. Well, that's one you're getting consistent feedback on: the same as with any other bit of Python expression syntax, don't add redundant line noise unless it serves a purpose (which, for parens, is pretty much limited to forcing or clarifying evaluation order in expressions with multiple operators).
"They're optional" is no reason for adding them; "they're optional" precisely in recognition of that adding them serves no purpose in these common cases. They're just noise here.