[Python-Dev] (name := expression) doesn't fit the narrative of PEP 20

Tim Peters tim.peters at gmail.com
Wed Apr 25 19:55:56 EDT 2018


[Tim]
>>>> To my eyes, this is genuinely harder to follow, despite its relative brevity:
>>>>
>>>>         while total != (total := total + term):

[Antoine]
>>> Does it even work?  Perhaps if the goal is to stop when total is NaN,
>>> but otherwise?

[Chris]
>> Yes, it does, because the first "total" is looked up before the
>> rebinding happens. It's 100% unambiguous to the compiler... but still
>> pretty unclear to a human. And I think the multiple use of 'total' is
>> to blame for that. So I agree with Tim that this particular example is
>> better in longhand.

[Antoine]
> "Better" is an understatement :-(  Now that I understood it (thanks
> for the explanation),

Ah, sorry - I had no idea it was the "left to right evaluation" part
you weren't seeing.  Next time explain why you think something is
broken?


> the shorthand version appears completely bonkers.

I wouldn't go that far, but I already said I wouldn't write it that way.

However, without looking at real code, people are just flat-out
guessing about how bad - or good - things _can_ get, no matter how
confident they sound.

So at least give me credit for presenting the _worst_ brief
binding-expression example you've seen too ;-)


More information about the Python-Dev mailing list