Iterator class to allow self-restarting generator expressions?

John O'Hagan research at
Sun Mar 1 16:20:28 CET 2009

Inspired by some recent threads here about using classes to extend the 
behaviour of iterators, I'm trying to replace some some top-level functions 
aimed at doing such things with a class.

So far it's got a test for emptiness, a non-consuming peek-ahead method, and 
an extended next() which can return slices as well as the normal mode, but 
one thing I'm having a little trouble with is getting generator expressions 
to restart when exhausted. This code works for generator functions:

class Regen(object):
    """Optionally restart generator functions"""
    def __init__(self, generator, options=None, restart=False):
        self.gen = generator
        self.options = options
        self.gen_call = generator(options)
        self.restart = restart
    def __iter__(self):
        return (self)
    def next(self):
        except StopIteration:
            if self.restart:
                self.gen_call = self.generator(self.options)

used like this:

def gen():
    for i in range(3):
        yield i

reg = Regen(gen, restart=True)

I'd like to do the same for generator expressions, something like:

genexp = (i for i in range(3))

regenexp = Regen(genexp, restart=True)

such that regenexp would behave like reg, i.e. restart when exhausted (and 
would only raise StopIteration if it's actually empty). However because 
generator expressions aren't callable, the above approach won't work.

I suppose I could convert expressions to functions like:

def gen():
    genexp = (i for i in range(3))
    for j in genexp:
        yield j

but that seems tautological.
Any clues or comments appreciated.


More information about the Python-list mailing list