[python]@cached_property缓存装饰器

cached_property缓存装饰器

class cached_property(object):
    """
    Decorator that converts a method with a single self argument into a
    property cached on the instance.
    Optional ``name`` argument allows you to make cached properties of other
    methods. (e.g.  url = cached_property(get_absolute_url, name='url') )
    """
    def __init__(self, func, name=None):
        self.func = func
        self.__doc__ = getattr(func, '__doc__')
        self.name = name or func.__name__
 
    def __get__(self, instance, cls=None):
        if instance is None:
            return self
        res = instance.__dict__[self.name] = self.func(instance)
        return res

cached_property主要实现的功能是,user.getWorkYear第一次会进行计算,计算完之后把实例user的__dict__['getWorkYear']设置为计算后的值。下次读值的时候会直接从__dict__['getWorkYear']取结果,避免了多次计算。
使用限制:只能用于只带默认参数的类

不使用的例子

class User(object):
    def __init__(self, age=0):
        self.age=age
    
    def getWorkYear(self):
        return 65-self.age


user=User(20)
print(user.getWorkYear)     #<bound method User.getWorkYear of <__main__.User object at 0x00000000031A3C88>>
print(user.getWorkYear())   #45
print(user.__dict__)  #{'age': 20}
print(user.getWorkYear) #<bound method User.getWorkYear of <__main__.User object at 0x00000000031A3C88>>

使用的例子

from django.utils.functional import cached_property

class User(object):
    def __init__(self, age=0):
        self.age=age

    @cached_property
    def getWorkYear(self):
        return 65-self.age


user=User(20)
print(user.getWorkYear)    #45
print(user.getWorkYear())  #error
print(user.__dict__)  #{'age': 20, 'getWorkYear': 45}
print(user.getWorkYear) #45

user.getWorkYear -> __get__ -> 从实例字典(user.__dict__`获取 -> 如果没有则保存到字典并调用实际方法返回

转载于:https://www.cnblogs.com/faithfu/p/10365868.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值