Memoization and encapsulation
- From: Steven D'Aprano <steve@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 31 Dec 2005 15:23:26 +1100
I was playing around with simple memoization and came up with something
like this:
_cache = {}
def func(x):
global _cache
if _cache.has_key(x):
return _cache[x]
else:
result = x+1 # or a time consuming calculation...
_cache[x] = result
return result
when it hit me if I could somehow bind the cache to the function, I could
get rid of that pesky global variable.
I tried this:
>>> def func(x):
.... try:
.... func.cache
.... except AttributeError:
.... func.cache = {}
.... if func.cache.has_key(x):
.... return func.cache[x]
.... else:
.... result = x + 1
.... func.cache[x] = result
.... return result
and it works as expected, but it lacks elegance.
Instead of using a function, I can also use a new-style class as if it
were a function:
>>> class Func(object):
.... cache = {}
.... def __new__(self, x):
.... if self.cache.has_key(x):
.... return self.cache[x]
.... else:
.... result = x+1
.... self.cache[x] = result
.... return result
and again it works, but I can't help feeling it is an abuse of the class
mechanism to do this.
What do folks think? Is there a better way?
--
Steven.
.
- Follow-Ups:
- Re: Memoization and encapsulation
- From: Just
- Re: Memoization and encapsulation
- From: Raymond Hettinger
- Re: Memoization and encapsulation
- From: Raymond Hettinger
- Re: Memoization and encapsulation
- From: bonono
- Re: Memoization and encapsulation
- Prev by Date: why writing list to file puts each item from list on seperate line?
- Next by Date: Re: why writing list to file puts each item from list on seperate line?
- Previous by thread: why writing list to file puts each item from list on seperate line?
- Next by thread: Re: Memoization and encapsulation
- Index(es):
Relevant Pages
|