[New-bugs-announce] [issue43143] Allow multiple assignment (i.e. tuple on LHS) in walrus operator
report at bugs.python.org
Sat Feb 6 03:13:07 EST 2021
New submission from Paul Sokolovsky <pfalcon at users.sourceforge.net>:
Currently (CPython 3.10.0a4) having a tuple on left-hand side of assignment expression/operator (aka walrus operator) is not allowed:
>>> ((a, b) := (1, 2))
File "<stdin>", line 1
((a, b) := (1, 2))
SyntaxError: cannot use assignment expressions with tuple
As far as can tell, there's no explicit consideration of this case in the PEP572. There closest it has is under the https://www.python.org/dev/peps/pep-0572/#differences-between-assignment-expressions-and-assignment-statements section, "Iterable packing and unpacking (both regular or extended forms) are not supported".
But note that the usecase presented above is better seen as "parallel assignment", not as "iterable unpacking".
Next issue: PEP572 puts heavy emphasis on the "named expression" usecase. I would like to emphasize, that "naming an expression" seems like *just one of usecases* for assignment operator. First and foremost assignment operator is, well, assignment.
With that in mind, ((a, b) := (1, 2)) is compatible with "naming expressions" idea, it "gives names" to multiple expressions at once.
There's a temptation to suggest that the above could be rewritten as:
((a := 1), (b := 2))
, and for the "naming" usecase, that would be true. But as noted above, "naming" is just *one* of the usecases. Following can't be "sequentialized" like that:
((b, a) := (a, b))
And for as long as someone considers the following acceptable:
func(a := val)
There's little point to prohibit the following:
min((b, a) := (a, b))
And that's the main argument - consistency between assignment statement and assignment operator. For as long as this is allowed:
a, b = b, c
it makes sense to allow:
((a, b) := (b,c))
(Extra parens account for different in grammar and precedence for the operator).
This issue is posted to capture discussion initially posted to python-ideas/python-dev:
, to serve as a reference to whoever may be searching the bugtracker for this case.
components: Interpreter Core
title: Allow multiple assignment (i.e. tuple on LHS) in walrus operator
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce