Odd closure issue for generators

Brian Quinlan brian at sweetapp.com
Thu Jun 4 21:34:41 EDT 2009

Gabriel Genellina wrote:
> En Thu, 04 Jun 2009 18:40:07 -0300, Brian Quinlan <brian at sweetapp.com> 
> escribió:
>> This is from Python built from the py3k branch:
> It's not new; same thing happens with 2.x
> A closure captures (part of) the enclosing namespace, so names are 
> resolved in that environment even after the enclosing block has finished 
> execution.
> As always, the name->value evaluation happens when it is required at 
> runtime, not earlier ("late binding"). So, in the generator expression 
> (lambda : i for i in range(11, 16)) the 'i' is searched in the enclosing 
> namespace when the lambda is evaluated, not when the lambda is created.

OK, I talked myself into agreeing that it is better for the generator 
comprehension to share a context amongst every enclosed generator 
expression (rather than having one context per generator expression) for 
consistency reasons.



More information about the Python-list mailing list