0

What I want is cache decorator for a method that works like functools.cached_property. cached_property caches the value of a property by adding it to the object __dict__, not to some global dict. Therefore, the object need not be hashable, as the "cache" (it's only one value) is stored in the object itself.

Now I would like the same thing for a method. So, something like a mechanism that creates a cache dict for the method in the object and memoizes method calls there. I know there are functools.cache and functools.lru_cache, but they require the class object to be hashable, as the method is just treated as a function with first argument self.

I have looked quite a bit, the best thing I could find is the cached_method package, but it only has very little stars. So either nobody needs this, there is a more popular implementation which I just can't find or this is trivial to implement in a few lines of code.

Does anyone know other more popular implementations?

Darkdragon84
  • 539
  • 5
  • 13
  • Does this help at all? [Python functools lru_cache with instance methods](https://stackoverflow.com/questions/33672412/python-functools-lru-cache-with-instance-methods-release-object) – JonSG Jul 07 '23 at 16:00
  • Is there an issue with adding a `__hash__` method to allow the use of lru_cache? – Guy Gangemi Jul 17 '23 at 07:37

0 Answers0