def memoized(f):
    cache = {}

def ret(*args):
    if args not in cache:
        cache[args] = f(*args)
if isinstance(cache[args], (GeneratorType, Tee)):
    cache[args], r = tee(cache[args])
return r
return cache[args]
return ret
Can I memoize a Python generator?