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:

  1. https://stackoverflow.com/questions/4431703/python-resettable-instance-method-memoization-decorator
  2. https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
  3. http://pythonhosted.org/cachetools/
  4. 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]
            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.


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!


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.