A question about generators

David Eppstein eppstein at ics.uci.edu
Mon Mar 18 19:34:00 EST 2002


In article <mailman.1016494109.20996.python-list at python.org>,
 "Delaney, Timothy" <tdelaney at avaya.com> wrote:

> > I have an application where I think either an iterator or a 
> > generator would
> > be ideal, but I have to be able to reset whichever I use.  The paper
> > "What's New in Python 2.2" says that there's no way to reset 
> > an iterator,
> > so that's out.  Is there a straight-forward way to reset a generator,
> > though?
> 
> That's not actually correct. There is no general way to reset and iterator
> or generator, but you can make a custom iterator or generator that can be
> reset.

You could create the generator inside some larger scope, and communicate 
with it by changing variables in that scope.  Ok, actually it's not 
possible to change a variable from outside that variable's scope (why??) 
but it is possible to have a list or dictionary within a scope and 
change one of its cells.

Or you can make a new generator every time you would want to reset it.

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list