revive a generator

Yingjie Lan lanyjie at yahoo.com
Fri Oct 21 04:02:53 EDT 2011


----- Original Message -----

> From: Paul Rudin <paul.nospam at rudin.co.uk>
> The language has no explicit notion of a request to "revive" a
> generator. You could use the same syntax to make a new generator that
> yeilds the same values as the one you started with if that's what you
> want. 
> 
> As we've already discussed if you want to iterate several times over the
> same values then it probably makes sense to compute them and store them
> in e.g. a list (although there are always trade-offs between storage use
> and the cost of computing things again).
> 

Oops, my former reply has the code indentation messed up 
by the mail system. Here is a reformatted one:


What if the generator involves a variable from another scope,
and before re-generating, the variable changed its value.
Also, the generator could be passed in as an argument,
so that we don't know its exact expression.

>>> vo = 34
>>> g = (vo*x for x in range(3))
>>> def myfun(g):
            for i in g: print(i)
            vo  += 3
            revive(g) #best if revived automatically
            for i in g: print(i)
>>> myfun(g)


Yingjie



More information about the Python-list mailing list