[Python-ideas] PEP 572: Assignment Expressions (post #4)

Tim Peters tim.peters at gmail.com
Fri Apr 13 00:52:35 EDT 2018


[David Mertz <mertz at gnosis.cx>]
> Yes, I have not read all the iterations of the PEP, and none of them
> extremely closely. I had thought it "obvious" that ':=' should have a very
> high operator precedence.

But you don't really believe that ;-)  That's the rub.  In your specific

    None if var:= function() is None else var.method()

example it was obvious := should have very high precedence _there_,
but in an earlier

    _, x1, x2 = (D := b**2 - 4*a*c), (-b + sqrt(D))/2, (-b - sqrt(D))/2

it was just as obvious that you wanted := to have a very low
precedence _there_ (else D would be bound to the value of plain old
`b`).

I don't at all mean to be picking on you there - I suffer the same
illusions.  It appears that "very low precedence" is actually wanted
in nearly all examples, _except_ when comparison is involved.

So maybe := "should have" precedence between comparison operators (<=,
is, etc) and bitwise OR (|).

Then both your examples above would do what you expected them to do
(and what I expected them to do at first glance).

OTOH, that wouldn't be at all like C, whose precedence rules Python
largely follows (only the comma operator has lower precedence than
assignment in C), and - worse - wouldn't be at all like Python
assignment statements appear to behave.


> But of course if it doesn't then expressions like
> the one I proposed could mean something quite different.

Whether very high or very low, one of your two examples above is screwed.


> I find the third hand argument rather compelling. All those potentially
> required parentheses turn elegant looking code into an ugly mess.

It is a hangup for me - but I'm not yet sure how severe.


More information about the Python-ideas mailing list