描述器类
作者:互联网
(1)描述器类
class Field:
# 设置描述器类对象属性时调用
def __set__(self, instance, value):
print('-------__set__---------')
self.value = value
# 获取描述器对象属性时调用
def __get__(self, instance, owner):
print('-------__get__---------')
return self.value
# 删除描述器类对象属性时调用
def __delete__(self, instance):
print('-------__delete__---------')
self.value = None
class Model:
name = 100
attr = Field()
m1 = Model()
m1.attr = 200
# print(m1.name)
print(m1.attr)
del m1.attr
print(m1.attr)
(2)输出结果:
-------__set__---------
-------__get__---------
200
-------__delete__---------
-------__get__---------
None
标签:__,器类,self,value,print,m1,-------__,描述 来源: https://www.cnblogs.com/bigcoolcool/p/16181905.html