版权声明:17602128911 https://blog.csdn.net/bus_lupe/article/details/86362058
class Lazyproperty:
def __init__(self, func):
self.func = func
def __get__(self, instance, owner):
return self.func(instance)
class Room:
# area = Lazyproperty(arg)
def __init__(self, name, width, length):
self.name = name
self.width = width
self.length = length
# @property => area = property(area)
# @property
# @Lazyproperty => area = Lazyproperty(area); 返回的是Lazyproperty类的一个实例对象
# 装饰器会执行Lazyproperty类的__init__函数
@Lazyproperty
def area(self):
return self.width * self.length
bedroom = Room('卧室', 5, 3)
# 类 > 数据描述符 > 实例对象 > 非数据描述符
# 会执行修饰符类Lazyproperty的__get__方法
print(bedroom.area)