Odd closure issue for generators

Terry Reedy tjreedy at udel.edu
Sat Jun 6 00:06:09 CEST 2009


Brian Quinlan wrote:
>
> Sorry, I wasn't as precise as I should have been.
> 
> If you consider this example:
> (<expr> for x in y)
> 
> I thought that every time that <expr> was evaluated, it would be done in 
> a new closure with x bound to the value of x at the time that the 
> closure was created.
> 
> Instead, a new closure is created for the entire generator expression 
> and x is updated inside that closure.

Thanks you for explaining your confusion.  Knowing what sort of 
other-language-baggage people are being mislead by can only help in 
explaining Python.  But here is my question.  In Python,

g = (<expr> for x in iterable)

is essentially an abbreviation for, and means the same as

def _(it):
   for x in it:
     yield <expr>
g = _(iterable)
del _

Are there language in which a similar construct has an essentially 
different meaning?

Terry Jan Reedy




More information about the Python-list mailing list