Re: 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

Re: Memoization and encapsulation

From: <skip@pobox.com>
Date: Sat Dec 31 2005 - 11:44:10 CET

    just> I actually prefer such a global variable to the default arg
    just> trick. The idiom I generally use is:

    just> _cache = {}
    just> def func(x):
    just> result = _cache.get(x)
    just> if result is None:
    just> result = x + 1 # or a time consuming calculation...
    just> _cache[x] = result
    just> return result

None of the responses I've seen mention the use of decorators such as the
one shown here:

    http://wiki.python.org/moin/PythonDecoratorLibrary

While wrapping one function in another is obviously a bit slower, you can
memoize any function without tweaking its source.

Skip
Received on Tue Jan 3 03:28:50 2006