New submission from Paul Sokolovsky <[email protected]>:
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:
https://mail.python.org/archives/list/[email protected]/thread/6IFDG7PAFPHVPGULANOQDAHP2X743HCE/
https://mail.python.org/archives/list/[email protected]/thread/X3LNCCO6PHTLAQXHEAP6T3FFW5ZW5GR5/
https://mail.python.org/archives/list/[email protected]/thread/6IFDG7PAFPHVPGULANOQDAHP2X743HCE/
, to serve as a reference to whoever may be searching the bugtracker for this
case.
----------
components: Interpreter Core
messages: 386551
nosy: pfalcon
priority: normal
severity: normal
status: open
title: Allow multiple assignment (i.e. tuple on LHS) in walrus operator
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue43143>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com