
Hi Paul, I suggest that you just go straight to the PEP phase. --Guido On Thu, Feb 4, 2021 at 11:54 PM Paul Sokolovsky <pmiscml@gmail.com> wrote:
Hello,
Everyone knows how hard to find a compelling usecase for the assignment expression operator (":=", colloquially "walrus operator"). https://www.python.org/dev/peps/pep-0572/ examples never felt compelling and we all remember the split it caused.
I finally found a usecase where *not* using assignment expression is *much* worse than using it. I'm working on SSA (Static Single Assignment, https://en.wikipedia.org/wiki/Static_single_assignment_form) conversion of Python programs, where there's a need to "join" dataflow of values from different control flow paths using a special function (Phi function). This "joining" itself creates a new variable, and of course, the original variable was used in an expression. We've got assignment in expression, assignment expression operator to the rescue!
With it, a simple loop like:
---- a = 0 while a < 5: a += 1 ----
becomes:
---- a0 = 0 while (a1 := phi(a0, a2)) < 5: a2 = a1 + 1 ----
So far, so good. But semantics of Phi function is parallel assignment. No problem with Python either, "a, b = b, c" is exactly parallel assignment. So, let's try example with 2 variables:
---- a = 0 b = 10 while a < 5: a += 1 b += 1 ----
becomes:
---- a0 = 0 b0 = 10 while ((a1, b1) := phi([a0, a2], [b0, b2]))[0] < 5: a2 = a1 + 1 b2 = b1 + 1 ----
But oops:
SyntaxError: cannot use assignment expressions with tuple
To reproduce in the REPL:
----
((a, b) := (1, 2)) File "<stdin>", line 1 SyntaxError: cannot use assignment expressions with tuple
Why this accidental syntactic gap? Why assignment statement can do parallel assignment with a tuple on LHS, and assignment operator suddenly can't?
Why the adhoc naming and conceptual shift on the AST level, when PEP572 explicitly talks about *assignment operator*, but corresponding node on the AST level is called NamedExpr? Why look at assignment expression as "name of expression" instead of assignment expression per se?
It's of course not a problem to recast:
NamedExpr(expr target, expr value)
to
NamedExpr(expr* target, expr value)
in the ASDL (and it works out of the box), the point is that it should have been ExprAssign from the start (folloing the AugAssign and AnnAssign tradition).
-- Best regards, Paul mailto:pmiscml@gmail.com _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/6IFDG7PA... Code of Conduct: http://python.org/psf/codeofconduct/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>