[Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part)
On Wed, Jun 27, 2018 at 12:01 PM, Eric V. Smith <eric at trueblade.com> wrote:
> >>> def test():
> >>> spam = 1
> >>> ham = 2
> >>> vars = [key1+key2 for key1 in locals() for key2 in locals()]
> >>> return vars
> >>> Wanna guess what that's gonna return?
Guessing aside, messing around with locals() isn't really helpful for the
usual case of common code.
But the real problem I see with all of this the distinction between
generator expressions and comprehensions:
an assignment expression occurring in a list, set or dict comprehension or
in a generator expression (below collectively referred to as
"comprehensions") binds the target in the containing scope, honoring a
nonlocal or global declaration for the target in that scope, if one exists.
For the purpose of this rule the containing scope of a nested comprehension
is the scope that contains the outermost comprehension. A lambda counts as
a containing scope.
It seems everyone agrees that scoping rules should be the same for
generator expressions and comprehensions, which is a good reason for
python3's non-leaking comprehensions:
In [*5*]: i = 0
In [*6*]: l = [i *for* i *in* range(3)]
In [*7*]: i
In [*8*]: i = 0
In [*9*]: g = (i *for* i *in* range(3))
In [*10*]: i
In [*11*]: *for* j *in* g:
In [*12*]: i
so comprehensions and generator expressions behave differently -- not great.
In [*4*]: i = 0
In [*5*]: l = [i *for* i *in* range(3)]
In [*6*]: i
In [*7*]: g = (i *for* i *in* range(3))
In [*8*]: i
In [*9*]: list(g)
Out[*9*]: [0, 1, 2]
In [*10*]: i
The loop name doesn't "leak" and comprehensions and generator expressions
are the same this regard -- nice.
So what about:
l = [x:=i for i in range(3)]
g = (x:=i for i in range(3))
Is there any way to keep these consistent if the "x" is in the regular
Note that this thread is titled "Informal educator feedback on PEP 572".
As an educator -- this is looking harder an harder to explain to newbies...
Though easier if any assignments made in a "comprehension" don't "leak out".
Which does not mean that we'd need a "proper" new local scope (i.e.
locals() returning something new) -- as long as the common usage was
>> I'm not singling out Chris here, but these discussions would be easier
> >> to follow and more illuminating if the answers to such puzzles were
> >> presented when they're posed.
well, I think the point there was that it wasn't obvious without running
the code -- and that point is made regardless of the answer.
Christopher Barker, Ph.D.
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...