zoukankan      html  css  js  c++  java
  • 利用类装饰器自定制property实现延迟计算

    class LazyProperty:
    
        '''
        hello,我是非数据描述符(没有定义__set__,不然是大哥数据描述符了--!)
        '''
    
        def __init__(self, func):
            print('>>>>>>>>>>这是LazyProperty的初始化方法__init__')
            # print(func)
            # print(func.__name__)
            self.func = func
    
        def __get__(self, instance, owner):
            print('》》》》》》这是__get__方法')
    
            # 类调用时instance为None,这时返回CrazyProperty对象
            if instance is None:
                return self
            res = self.func(instance)
            # 把结果保存到实例属性字典,可以实现延迟计算。即函数不用来回多次调用,提高效率
            setattr(instance, self.func.__name__, res)
            return res
    
    
    class Room:
        def __init__(self, name, width, height):
            self.name = name
            self.width = width
            self.height = height
    
        # 类也可以作为装饰器,而且还是一个描述符呢,有意思吧
        @LazyProperty  # area = LazyProperty(area) 函数属性area被CrazyProperty代理了
        def area(self):
            return self.width * self.height
    
    
    r1 = Room('toilet', 1, 1)
    print(r1.area)
    print(r1.area)
    print(r1.area)
    print(r1.area)

    运行结果

    >>>>>>>>>>这是LazyProperty的初始化方法__init__
    》》》》》》这是__get__方法
    1
    1
    1
    1

    由结果可以看出,__get__只运行了一次,结果保存到了实例的属性字典,后面的调用直接在实例属性字典里取值。

  • 相关阅读:
    字符串 高精度计算
    JAVA Socket编程 课堂作业
    图论 Floyd算法
    天梯赛题解 L1-049 天梯赛座位分配
    天梯赛题解 -L1-039 古风排版
    HDU 5558 后缀数组
    HDU 6194 后缀数组
    HDU 5769 后缀数组
    HDU 4691 后缀数组+RMQ
    HDU 4135 容斥原理
  • 原文地址:https://www.cnblogs.com/jeavy/p/10055344.html
Copyright © 2011-2022 走看看