I have some computationally intensive functions in my python script that I would like to cache. I went looking for solutions on stack overflow and found lots of links:
- https://stackoverflow.com/questions/4431703/python-resettable-instance-method-memoization-decorator
- https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
- http://pythonhosted.org/cachetools/
- https://pythonhosted.org/Flask-Cache/ (I’ve used this one for flask applications, but this one is not a flask application).
In the end, I ended up pasting this into my program. It seems simple enough — and works fine.
class memoized(object):
'''Decorator. Caches a function's return value each time it is called.
If called later with the same arguments, the cached value is returned
(not reevaluated).
'''
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
if not isinstance(args, collections.Hashable):
return self.func(*args)
if args in self.cache:
return self.cache[args]
else:
value = self.func(*args)
self.cache[args] = value
return value
def __repr__(self):
'''Return the function's docstring.'''
return self.func.__doc__
def __get__(self, obj, objtype):
'''Support instance methods.'''
return functools.partial(self.__call__, obj)
However, I am wondering if there is a cannonical best practice in Python. I guess I assumed that there would be a very commonly used package to handle this and am confused about why this does not exist. http://pythonhosted.org/cachetools/ is only on version .6 and the syntax is more complex than simply adding a @memoize decorator, like in other solutions.
1
There is no canonical, uniquely Pythonic way to do this. None of which I am aware, at any rate–and I’m speaking as someone who has looked, and who is the author of a successful memoizing package.
However, I believe your lack of found prior art may be a terminology issue as much as anything. You asked for caching. That is a proper term, but it’s overly broad. Caching the results of a particular function call or activity for later use is more specifically referred to as memoizing or memoization. And indeed there are many memoizing packages available from the community, as well as many recipes (for example, this one). I have also seen memoizng functions in many multi-purpose utility packages. Many of them are mature, battle-hardened, and constantly used in production–not mere “version 0.6 code.”
Why memoizing is not more canonically or idiomatically handled I cannot say. Perhaps because there are various ways to accomplish it with differing virtues and tradeoffs. Or perhaps because there are already so many different approaches in use. I often find features–“flatten a list of list” is another–that other language communities eagerly converge around but that the Python community or powers that be seem to prefer handling as recipes rather than committing to a specific API. In any case, if your code works, welcome to the ranks of successful memoizers!
Update
Since the Python 3 standard library (for 3.2 and later) includes an lru_cache
decorator (documentation here), I’d have to say that looks like a late-breaking attempt to standardize the most common memoization use case. That it came so late in Python’s evolution is probably why there’s no common solution, but for new code, that’s as close to canonical as you’re going to find.
1
Because an instance method is allowed to use self attributes, and in particular modify them, you can’t guarantee correctness of an arbitrary memoized instance method. Additionally, your implementation adds an implicit constraint on your object to be hashable, and cache hit depending of that hash, you are limited on classes you can use it on and fields you can add to classes having a memoized method (or inheriting one).
For these reasons, if you need to memoize a function it is better design to turn the expensive instance method into a static method, explicitly passing to it necessary object attributes as arguments. This frees your hands on class design, and can improve cache hit. This also helps simplifying and generalizing the memoizing code. Fine-grain implementations exists for this desing, sometimes letting you customize cache size, duration / invalidation methods, guaranteeing thread safety, persistance…