number generator

Anton Vredegoor anton.vredegoor at gmail.com
Wed Mar 14 18:13:38 CET 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