Py学习  »  Python

Python类方法的备忘录,我们可以/应该使用functools缓存吗?

Ben • 3 年前 • 333 次点击  

据我所知,functools@cache decorator将记录这些参数。 然而,对于类的实例方法,参数包括“self”。 这会对性能产生影响吗?

class Test:
    def __init__(self):
        self.cache = {}

    def test(self, a, b):
        result = self.cache.get((a,b))
        if result is None:
            result = a + b
            self.cache[(a,b)] = result
        return result

上述操作是否会比简单地用@functools修饰test()更糟糕。隐藏物

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128613
 
333 次点击