Odd closure issue for generators
Michele Simionato
michele.simionato at gmail.com
Fri Jun 5 21:24:19 EDT 2009
On Jun 6, 12:06 am, Terry Reedy <tjre... at udel.edu> wrote:
> 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
Yes, most functional languages have the concept of streams.
You can even define a stream-comprehension that looks like
Python generator comprehension but it is an essentially different
thing. See for instance
http://www.artima.com/weblogs/viewpost.jsp?thread=251159
More information about the Python-list
mailing list