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?