zoukankan      html  css  js  c++  java
  • python @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

    不使用的例子:

    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

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

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

  • 相关阅读:
    基于python+django+mysql的接口测试平台
    firefox没有装在C盘,webdriver启动firefox时报错
    Python知识点面试题
    Python面试题整理
    Python程序猿面试杂谈
    Python面试-websocket及web框架
    Python面试-DB相关
    Python面试简介及并行并发
    flink-demo2
    flink-table demo
  • 原文地址:https://www.cnblogs.com/miaoweiye/p/12661111.html
Copyright © 2011-2022 走看看