Memoizing decorator

Scott David Daniels scott.daniels at acm.org
Mon Dec 5 21:47:40 EST 2005


Daishi Harada wrote:
> ...  I've managed to get these working for both
> functions and methods by wrapping them in
> yet another function (the following is for the
> cookbook example, replacing 'cachedmethod'):
> 
> ---
> def memoize(function):
>     im = Memoize(function)
>     def fn(*args, **kwargs):
>         return im(*args, **kwargs)
>     return fn
> ---
But, this is equivalent to:
   def memoize(function):
       im = Memoize(function)
       return im

Which is (in turn) equivalent to:
   memoize = Memoize

So you can just use
     @Memoize
     def function (....
         ....


--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list