Restart generator when it is exhausted.

Duncan Booth duncan.booth at invalid.invalid
Tue Apr 28 06:41:50 EDT 2009


Lacrima <Lacrima.Maxim at gmail.com> wrote:

> If it is not possible what are common techniques to use iterator or
> generator objects that allow restarting when it is needed?

The usual thing if you want to use the generator's output more than once  
would be to convert the generator to a list, then you can iterate over it 
as often as you want.

>>> a = ['a', 'b', 'c']
>>> g = (i for i in a)
>>> restartable = list(g)

If you want the output of the generator to potentially change each time you 
iterate then you need to create a new generator.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list