number generator

Anton Vredegoor anton.vredegoor at gmail.com
Wed Mar 14 13:13:38 EDT 2007


Anton Vredegoor wrote:

> def memoize(fn):
>       cache = {}
>       def proxy(*args):
>           try: return cache[args]
>           except KeyError: return cache.setdefault(args, fn(*args))
>       return proxy

Sorry this doesn't work in this case. This works:

def memoize(fn):
      cache = {}
      def proxy(*args):
          try: return cache[args]
          except KeyError: return cache.setdefault(args, list(fn(*args)))
      return proxy

But then we lose all speed advantages from memoizing so
it's no good either.

A.



More information about the Python-list mailing list