[Python-Dev] PEP 572: Assignment Expressions
Christoph Groth
christoph at grothesque.org
Sat Apr 21 18:44:31 EDT 2018
Tim Peters wrote:
> [Christoph Groth <christoph at grothesque.org>]
> > Still, it seems weird to have two different ways of binding names in
> > the language where one would be sufficient (i.e. the old one would
> > remain only for backwards compatibility). From the point of view of
> > someone who's new to the language that's two things to learn instead
> > of just one.
>
> But they're very different in a key respect. the value of an
> assignment expression is the value assigned. Asking "what's the value
> of a statement?" doesn't even make sense in Python (whether an
> assignment statement or any other kind of statement).
There are also no function call statements in Python. People are
happily using function call expressions as statements when not
interested in their value.
I hope to have shown [1] that the same could be done for assignments. A
consistent value can be defined for any assignment statement. So, all
assignment statements could be redefined as expressions and the language
would continue to work and even be (perfectly?) backwards-compatible.
Syntax-wise, if replacing = by := everywhere is unthinkable, as it
seems, there's still the possibility (not completely ruled out by Guido
;-) to use = for assignment expressions but require extra parens for
safety.
Thus, it seems to me that redefining assignments as expressions
everywhere is a feasible, if radical, idea. Compared to a dedicated
syntax for "binding expressions" it would be conceptually simpler, but
would provide more possibilities to shoot oneself in the foot.
[1] https://mail.python.org/pipermail/python-dev/2018-April/152780.html
More information about the Python-Dev
mailing list