revive a generator

Chris Angelico rosuav at gmail.com
Thu Oct 20 21:51:38 EDT 2011


On Fri, Oct 21, 2011 at 12:46 PM, Yingjie Lan <lanyjie at yahoo.com> wrote:
>
> Thanks a lot to all who answered my question.
> I am still not sure why should we enforce that
> a generator can not be reused after an explicit
> request to revive it?

Here's an example of an explicit request to revive the generator:

>>> g = (x*x for x in range(3))
>>> for x in g: print x
0
1
4
>>> g = (x*x for x in range(3)) # revive the generator
>>> for x in g: print x #now this will work
0
1
4

ChrisA



More information about the Python-list mailing list