Re: Memoization and encapsulation
- From: skip@xxxxxxxxx
- Date: Sat, 31 Dec 2005 04:44:10 -0600
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
.
- References:
- Memoization and encapsulation
- From: Steven D'Aprano
- Re: Memoization and encapsulation
- From: Just
- Memoization and encapsulation
- Prev by Date: Re: python encoding bug?
- Next by Date: Re: py-cocoa?
- Previous by thread: Re: Memoization and encapsulation
- Next by thread: Re: Memoization and encapsulation
- Index(es):
Relevant Pages
|