Python对象 自定制property
1. 自定制property 初始版
利用 @语法,使类增加一个类属性, 然后利用描述符来实现
class lazyproperty: def __init__(self,func): self.func=func def __get__(self, instance, owner): print(instance) res=self.func(instance) return res class Room: def __init__(self,height,width): self.height=height self.width=width """ 1.使用@lazyproperty后, Room类里面增加了一个类属性, area=lazyproperty() 2.调用 r.area 就利用描述的的原理,去调用lazyproperty里面的 __get__方法 """ @lazyproperty # area=lazyproperty(area) def area(self): return self.height*self.width r=Room(20,12) print(r.area)