社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

Ben • 4 年前 • 439 次点击  

据我所知,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