[Python-Dev] PEP 572 semantics (original) (raw)
Steve Dower steve.dower at python.org
Wed Jul 4 14:50:53 EDT 2018
- Previous message (by thread): [Python-Dev] PEP 572 semantics
- Next message (by thread): [Python-Dev] PEP 572 semantics
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 04Jul2018 1021, Tim Peters wrote:
Same as now,
iis local to the synthetic nested function created for the genexp. The scope ofais determined by pretending the assignment occurred in the block containing the outermost (textually - static analysis) comprehension. In this case,a = anythingbefore thereturnwould establish thatais local tof, so that's the answer:ais local tof. Ifahad been declared global inf, thenain the genexp would be the same globala. And similarly ifahad been declared nonlocal.in <http://nonlocal.in>f.In all cases the scope resolution is inherited from the closest containing non-comprehension/genexp block, with the twist if that if a name is unknown in that block, the name is established as being local to that block. So your example is actually the subtlest case.
Okay, so as far as the specification goes, saying "assignment expressions in comprehensions get or create a cell variable in the defining scope and update its value" satisfies me just fine (or some other wording that more closely mirrors the actual behaviour - all my work here is on my own compiler, not the actual CPython one, and I don't know that they're identical).
I don't think this should be left assumed by the PEP. If it's likely to be a restriction on other implementations to say "cell variable", then say "For example, in CPython, ..."
> From the any()/all() examples, it seems clear that the target scope for > the assignment has to be referenced from the generator scope (but not > for other comprehension types, which can simply do one transfer of the > assigned name after fully evaluating all the contents).
I don't think that follows. It may in some cases. For example, [SNIP] While the list comprehension is executing, it needs to rebind f's
ion each iteration so that the call tog()on each iteration can seei's then-current value.
Good point. My statement above captures this nuance, as far as I'm concerned. (Same for the frame lifetime discussion, which I snipped).
Cheers, Steve
- Previous message (by thread): [Python-Dev] PEP 572 semantics
- Next message (by thread): [Python-Dev] PEP 572 semantics
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]