Restart generator when it is exhausted.

Lacrima Lacrima.Maxim at gmail.com
Tue Apr 28 05:54:38 EDT 2009


Hello!

I am quite new to Python and I have maybe simple (or maybe not)
question.

Is it possible to restart generator when it is exhausted?
For example:
>>> a = ['a', 'b', 'c']
>>> g = (i for i in a)
>>> g.next()
'a'
>>> g.next()
'b'
>>> g.next()
'c'
>>> g.next()
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    g.next()
StopIteration
>>>
What should I do to get the initial state of g? So if I do again g.next
() I receive 'a'.

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

With regards,
Max

(sorry if my English isn't very proper)



More information about the Python-list mailing list