[Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)

Tim Peters tim.peters at gmail.com
Tue Jul 3 01:47:33 EDT 2018

[Rob Cliffe]
> It's late to raise this,

By months, yes ;-)

> but what exactly are the objections to the syntax

> >         expr -> name  # or variations such as  expr => name

> > instead of

> >         name := expr

> >

> > The PEP mentions that this syntax does not have a problem that "as"

> > does, but does not list any downsides of it.

My guess:  it probably strikes too many as "excessive novelty",   These are
assignment expressions.  Python's assignment statements put the target at
the left.  Why change that?  ":=" is used for assignment in many more other
languages than "->" is.  Why fight that?

> It conforms to left-to-right evaluation, where name:=expr does not.

?  Only "expr" is evaluated, so left-to-right seems irrelevant here.  The
"evaluation" of a simple name as a binding target is a no-op (no code is
generated).  If you really do see this as a wart anyway, then it's
positively a Good Thing that it's exactly the same "wart" as in Python's
assignment statements.

> It (I would argue) reduces the asymmetry of the first use of a
> sub-expression in cases such as

> >     [ ( (f(x) -> y)**2, y**3, y**4) for x in iterable ]

> >  vs

> >     [ ( (y := f(x))**2, y**3, y**4) for x in iterable ]

> > because the first "y" is closer to the way it is used, viz "**2".

The first form reads a little better to me too, but not a lot better.  The
problem I have with variations of this example on its own (which comes up
surprisingly often with minor changes) is that it's clearer spelled today

    [(y**2, y**3, y**4) for y in map(f, iterable)]

Spelling that with either form of assignment expression reads significantly
worse than that to my eyes

But more importantly, it's expected that assignment expressions will be
used _most_ often to make some common `if` and `while` patterns briefer.
Hardly all.  Our eyes are already trained to "look at the far right end"
for the value being tested, and, e.g.,

    while data := sock.recv():

preserves that.  Especially in code that doesn't _always_ use assignment
expressions in such contexts (which is likely all significant blobs of
code), it would be visually jarring to have to "sometimes look in the
middle instead" to extract the important part of:

    while sock.recv() -> data:

"Look to the left for the name, look to the right for the value" is the
rule for assignment statements, assignment expressions, and `for` loop

But there's no "QED" here because this isn't a deductive science.  The
final answer is "because that's what Guido liked best" ;-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180703/5fd9cde5/attachment.html>

More information about the Python-Dev mailing list