[Python-Dev] PEP 572: Assignment Expressions

Sven R. Kunze srkunze at mail.de
Tue Apr 24 16:42:35 EDT 2018


On 23.04.2018 23:41, Tim Peters wrote:
> Why?  "Give the result of an expression a name" is already heavily
> used in Python - it's just that the _contexts_ in which it can be done
> are very limited now.

Which is a good thing IMO. It keeps stuff simple to reason about.
At least to me, the objective of an expression is to generate a
result, not to introduce new names. YMMV.

> After decades, CPython still does nothing of the sort, short of having
> eventually made, e.g., "None" and "True" and "False" reserved words so
> at least it can optimize uses of those.  It knows nothing at all about
> which library functions are pure - and there's no code in the
> implementation currently capable of exploiting such information even
> if it were known.  That remains a fantasy in CPython.

CPython optimizes on dicts pretty heavily and on a lot of other things 
as well.
Victor, e.g., is pretty prolific here when it comes to optimizing things 
for the 95% usecases.
Maybe, considering pure functions might be another step.

> Guido gave better ones, where binding expressions would allow to
> collapse arbitrarily deep levels of nesting to just one (if ... elif
> ... elif ... elif ...). My example only eliminated a single level of
> artificial indentation.  But my example did have the advantage of
> being taken verbatim from actual, working code ;-)

I like the example, but not the solution it proposes.

gcd(diff, n) is to me a perfect name, and please don't tell me g is 
better. ;)
To me it seems, that many people consider function_of_parameter a better 
name than function(parameter). IMO it isn't.
I've seen lots of code where people do things like foo_of_bar = 
bar['foo']. Because they don't want another dict access, or they think 
it better reflects the concept. But it does not as does not g. Writing 
gcd(diff, n) twice is not more repeating as is writing foo_of_bar twice. 
Because gcd is a pure function, gcd(diff, n) is just a synonym/name of 
the very same number. That it needs to be calced twice, is a detail, 
like a double dict access.

In the end, it's only optimizing what could a be done by a machine much 
more reliably.
Though, people now want to save that extra assignment line (and 
indentations) via syntax,
because they do that kind of optimizing and want to do it by hand for 
some reason.

Just my 2 cents to let you see where I'm coming from.




More information about the Python-Dev mailing list