Memoization and encapsulation
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Memoization and encapsulation

From: Steven D'Aprano <steve@REMOVETHIScyber.com.au>
Date: Sat Dec 31 2005 - 05:23:26 CET

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.
Received on Tue Jan 3 03:28:24 2006