Resetting generator object in Python Resetting generator object in Python python python

Resetting generator object in Python


Generators can't be rewound. You have the following options:

  1. Run the generator function again, restarting the generation:

    y = FunctionWithYield()for x in y: print(x)y = FunctionWithYield()for x in y: print(x)
  2. Store the generator results in a data structure on memory or disk which you can iterate over again:

    y = list(FunctionWithYield())for x in y: print(x)# can iterate again:for x in y: print(x)

The downside of option 1 is that it computes the values again. If that's CPU-intensive you end up calculating twice. On the other hand, the downside of 2 is the storage. The entire list of values will be stored on memory. If there are too many values, that can be unpractical.

So you have the classic memory vs. processing tradeoff. I can't imagine a way of rewinding the generator without either storing the values or calculating them again.


Another option is to use the itertools.tee() function to create a second version of your generator:

import itertoolsy = FunctionWithYield()y, y_backup = itertools.tee(y)for x in y:    print(x)for x in y_backup:    print(x)

This could be beneficial from memory usage point of view if the original iteration might not process all the items.


>>> def gen():...     def init():...         return 0...     i = init()...     while True:...         val = (yield i)...         if val=='restart':...             i = init()...         else:...             i += 1>>> g = gen()>>> g.next()0>>> g.next()1>>> g.next()2>>> g.next()3>>> g.send('restart')0>>> g.next()1>>> g.next()2