Reset a generator function?

Mark McEahern marklists at mceahern.com
Tue Sep 10 15:42:48 EDT 2002


[Robert Oschler]
> How can I reset a generator function so that it begins yielding
> values as it did when first called?

My braindead way of doing it is just to create another instance of the
generator:

$ python
Python 2.2.1 (#1, Jun 25 2002, 10:55:46)
[GCC 2.95.3-5 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import generators
>>> def mygen():
...     i = 0
...     while True:
...             i += 1
...             yield i
...
>>> x = mygen()
>>> x.next()
1
>>> x.next()
2
>>> x.next()
3
>>> x = mygen()
>>> x.next()
1
>>>

// m

-





More information about the Python-list mailing list