[Python-ideas] PEP 572 version 2: Statement-Local Name Bindings
Christoph Groth
christoph at grothesque.org
Fri Mar 23 12:34:09 EDT 2018
Disclaimer: I skimmed/searched through the PEP 572 threads (or should I
say "literature"?) and did not find a discussion of the following point.
If it has been discussed already, I'd be glad to be pointed to it.
I am aware that Python, in contrast to C-like languages, has chosen not
to treat assignments (=) as an expression with a value. The motivation
for this is to avoid bugs due to confusion with the equality operator
(==).
But wouldn't it be a good alternative to PEP 572 to *add* an assignment
operator that is an expression to Python, and is distinct from "=", for
example ":="? Then, instead of PEP 572's:
stuff = [[(f(x) as y), x/y] for x in range(5)]
one could write
stuff = [[(y := f(x)), x/y] for x in range(5)]
In difference to PEP 572, the variable y would not be statement-local,
which IMHO would be a welcome simplification. (PEP 572 introduces a
third class of variables to Python.)
Overall, it seems to me that introducing a new operator ":=" would serve
the same purpose as PEP 572, but in a simpler and arguably cleaner way,
while eliminating the risk of confusion with "==". The operator "="
would be left around, but could be deprecated in Python 5 and removed in
Python 6. It would certainly suit a language that is widely used in
education to sharpen the distinction between assignment and equality
operators.
Cheers,
Christoph
More information about the Python-ideas
mailing list