0

For a project, I would like to cache the results computed by a method. Some constraints:

  • I can't use Python3
  • I can't use 3rd party modules

What I've come up so far is this:

def cached_method(fun):
    """A memoize decorator for class methods."""
    @functools.wraps(fun)
    def get(self, *args, **kwargs):
        """Return the value if cached.
        Get it, cache it and return it if not.
        """
        try:
            self._cache[fun]
        except AttributeError:
            self._cache = {}
        except KeyError:
            pass

        return self._cache.setdefault(fun, {}).setdefault((args, tuple(kwargs.items())), fun(self, *args, **kwargs))

    return get

However, there's something wrong. In fact, using a minimal example, such as this:

class TestClass(object):

    @cached_method
    def method(self, p_arg):
        print 'sentinel'
        return p_arg

sut = TestClass()
sut.method(2)
sut.method(2)

You'll see the method is called twice ('sentinel' is printed twice)

Inspecting it via PDB I can't see what the problem is, given the second time _cache is effectively containing all what's needed.

Edit

As pointed out in the comments, the issue is with setdefault which calls the argument passed as default (the second) regardless whether the key is found or not.

To fix that problem, replace the return line above with:

results = self.cache.setdefault(fun, {})

try:
    ret = results[(args, tuple(kwargs.items()))]
except KeyError:
    ret = results[(args, tuple(kwargs.items()))] = fun(self, *args, **kwargs)

return ret
8
  • fun(self, *args, **kwargs) calls the function, even if that's the 2nd argument to setdefault. You have to check if the result exists in the cache, and only call the function if the cache lookup comes up empty.
    – Aran-Fey
    Apr 24, 2018 at 10:42
  • I think you mean: try: return self._cache[fun].
    – quamrana
    Apr 24, 2018 at 10:43
  • @Aran-Fey that's the issue - for some reason I thought setdefault didn't use the second argument (the default) if the key was there. Fixing that fixes the problem.
    – Jir
    Apr 24, 2018 at 11:14
  • @quamrana, not really, that was intentional - if you return at that point you return a dictionary of results, which is not what was intended. You could argue you can use those intermediate results later on, but that, IMO, was complicating the whole function.
    – Jir
    Apr 24, 2018 at 11:17
  • I've accepted to close the question as duplicate, but as mentioned above, I'll stick with the code used here, rather than using the fully-fledged lru_cache implementation, being more simple for my purpose.
    – Jir
    Apr 24, 2018 at 11:20

0

Browse other questions tagged or ask your own question.