Why does functools.lru_cache not cache __call__ while working on normal methods

倾然丶 夕夏残阳落幕 提交于 2020-04-16 05:14:21

问题


I have been trying to make functools.lru_cache instance specific as described in this answer, but their solution fails when used on the __call__ method.

class test:
    def __init__(self):
        self.method = lru_cache()(self.method)
        self.__call__ = lru_cache()(self.__call__)

    def method(self, x):
        print('method', end=' ')
        return x

    def __call__(self, x):
        print('__call__', end=' ')
        return x

b = test()
# b.method is cached as expected
print(b.method(1)) # method 1
print(b.method(1)) # 1

# __call__ is executed every time
print(b(1)) # __call__ 1
print(b(1)) # __call__ 1

So the results of __call__ are not getting cached when wrapped using this method. The cache on __call__ does not even register the function having been called, and unhashable values do not throw errors.

print(b.method.cache_info())
# CacheInfo(hits=1, misses=1, maxsize=128, currsize=1)
print(b.__call__.cache_info())
# CacheInfo(hits=0, misses=0, maxsize=128, currsize=0)

print(b.call({})) # __call__ {}
print(b.method({})) # ... TypeError: unhashable type: 'dict'

回答1:


This is due to the difference between class attributes and instance attributes. When accessing an attribute (such as method) python first checks for an instance attribute. If you have not assigned to self.method it will not find one. Then class attributes are checked, which is equivalent to self.__class__.method. The value of this function is not changed by assigning to self.method, that only updates the instance attribute.

However, b(1) becomes b.__class__.__call__(b, 1) which uses the original class definition of __call__ and b.__call__(1) will be cached the same way as method since it uses the instance definition.




回答2:


The original answer is really good.

I am attaching another solution of the problem. methodtools.lru_cache will work as you expect.

from methodtools import lru_cache

class test:
    @lru_cache
    def __call__(self, x):
        print('__call__', end=' ')
        return x

It requires to install methodtools via pip:

pip install methodtools



来源:https://stackoverflow.com/questions/53423121/why-does-functools-lru-cache-not-cache-call-while-working-on-normal-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!