class ListCachingIterable(object):

    def __init__(self, obj):
        self.vals = list(obj)

def __iter__(self):
    return iter(self.vals)

def cube_generator(max=1000):
    i = 0
while i < max:
    yield i * i * i
i += 1

def runit(iterable_factory):
    for i in range(5):
        for what in iterable_factory():
            pass

def puregen():
    runit(lambda : cube_generator())

def listtheniter():
    res = list(cube_generator())
runit(lambda : res)

def listcachingiterable():
    res = ListCachingIterable(cube_generator())
runit(lambda : res)

def asyougocachingiterable():
    res = AsYouGoCachingIterable(cube_generator())
runit(lambda : res)
Caching a generator