class C:
def __init__(self,size=100):
self.size = size
def getx(self):
return self.size
def setx(self,value):
self.size = value
def delx(self):
del self.size
x = property(getx,setx,delx)
#如果c是C的实例化,c.x将触发getx, x=value将触发setx, del c.x将触发delx。
>>>c=C() #类的实例化
>>>c.x #调用property的第1个方法
100
>>>c.x = 200 #调用property的第2个方法
>>>c.x
200
>>>del c.x #调用property的第3个方法
c.x
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 5, in getx
AttributeError: 'C' object has no attribute 'size'
来源:CSDN
作者:Rocky-zg
链接:https://blog.csdn.net/mtldswz312/article/details/103876265