def memodict(f):
 class memodict(defaultdict, ):
  def __missing__(self, key):
   ret = self[key] = f(key)
   return ret
 return memodict().__getitem__

def memodict ( f ) : class memodict ( defaultdict , ) : def __missing__ ( self , key ) : ret = self [ key ] = f ( key ) return ret return memodict ( ) . __getitem__
memoization decorator for a function taking a single argument .