[Python-ideas] Spelling of Assignment Expressions PEP 572 (was post #4)

Tim Peters tim.peters at gmail.com
Fri Apr 13 12:14:18 EDT 2018


[Chris]
> ...
> So I don't see much value in a "->" operator, except for the
> mere fact that it's different (and thus won't conflict in
> except/with); and the bulk of name bindings in Python put
> the name first.

It does give a natural solution to one of the problematic examples,
because as a very-low-precedence operator it would suck up "everything
to the left" instead of "everything to the right" as "the expression
part"::

    if f(x) -> a is not None:

can't be misunderstood, but:

    if a := f(x) is not None:

is routinely misunderstood.

On the other hand, if assignment expressions are expanded to support
all the forms of unpacking syntax (as Guido appears to favor), then
other cases arise:

    if f() -> a, b > (3, 6):

Is that:

    if ((f() -> a), b) > (3, 6):

or

    if (f() -> (a, b)) > (3, 6):

?

Which is an argument in favor of ":=" to me:  an assignment statement
can be pretty complex, and if an assignment operator can become just
as complex then it's best if it looks and works (as much as possible}
exactly like an assignment statement.

If someone writes

    if a, b := f() > (3, 6):

it's easy to explain why that's broken by referring to the assignment statement

    a, b = f() > (3, 6)

"You're trying to unpack a Boolean into a 2-tuple - add parens to
express what you really want."


More information about the Python-ideas mailing list