[Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)
Chris Barker - NOAA Federal
chris.barker at noaa.gov
Thu Jun 28 11:35:13 EDT 2018
Sent from my iPhone
> > So what about:
> > l = [x:=i for i in range(3)]
> > vs
> > g = (x:=i for i in range(3))
> > Is there any way to keep these consistent if the "x" is in the regular local scope?
> I'm not clear on what the question is. The list comprehension would bind ` l ` to [0, 1, 2] and leave the local `x` bound to 2. The second example binds `g` to a generator object, which just sits there unexecuted. That has nothing to do with the PEP, though.
> If you go on to do, e.g.,
> l = list(g)
> then, same as the listcomp, `l` will be bound to [0, 1, 2] and the local `x` will be left bound to 2.
OK, it has been said that the priority is that
Behave the same as
So we’re good there. And maybe it’s correct that leaving the running
of the gen_exp ‘till later is pretty uncommon, particularly for
If the execution of the gen_exp is put off, it really confuses things
— that name being changed would happen at some arbitrary tone, and at
least in theory, the gen_exp could be passed off to somewhere else in
the code, and be run or not run completely remotely from where the
name is used.
So while this is technically the same as the comprehension, it is not
the same as a generator function which does get its own scope.
And we should be clear how it will work — after all, in py2, the
handling of the looping name was handled differently in gen_exp vs
So I think a local scope for all comprehension-like things would be
the way to go.
But getting back to the original thread topic — python has a number of
places that you can only use expressions — adding the ability to bind
a name in all these places complicates the language significantly.
> Put a body B in a listcomp and any side effects due to executing B
Maybe it’s just me, but re-binding a name seems like a whole new
category of side effect.
More information about the Python-Dev