[Python-ideas] PEP 572: Statement-Local Name Bindings

Chris Angelico rosuav at gmail.com
Wed Feb 28 05:25:04 EST 2018


On Wed, Feb 28, 2018 at 8:55 PM, Matt Arcidy <marcidy at gmail.com> wrote:
> I appreciate that point as it is what I must be misunderstanding.
>
> I believe the performance speed up of [((f(x) as h), g(h)) for x
> range(10)] is that there are 10 calls to compute f, not 20.
>
> You can do this with a dictionary right now, at least for the example
> we're talking about:
> [(d[x], g(d[x])) for x in range(10) if d.update({x:f(x)}) is None]
>
> It's ugly but get's the job done.  The proposed syntax is leagues
> better than that, but just to give my point a concrete example.

Definitely ugly... if I saw that in code review, I'd ask "When is that
condition going to be false?". It also offers nothing that the "extra
'for' loop" syntax can't do better.

Memoization can only be done for pure functions. If the call in
question is actually, say, "next(iter)", you can't memoize it to avoid
duplicate calls. Possibly not the greatest example (since you could
probably zip() to solve all those sorts of cases), but anything else
that has side effects could do the same thing.

ChrisA


More information about the Python-ideas mailing list