__get__,__set__,__delete__

# class Str:
#     def __get__(self, instance, owner):
#         print('str__get__')
#     def __set__(self, instance, value):
#         print('str __set__')
#     def __delete__(self, instance):
#         print('str __del__')
# class People:
#     name=Str()
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#类属性大于数据描述符属性
# People.name
# People.name='wesley'  #不会触发__set__
# print(People.__dict__) 'name': 'wesley',在类的字典里
#数据描述符属性大于实例属性
# p1=People('wes',18)    对象p1操作name没有任何用
# print(p1.__dict__)
# str __set__
# {'age': 18}
#实例属性大于非数据属性
class Foo:
    def func(self):
        print('hahah')
f1=Foo()
# f1.func()
print(Foo.__dict__)
# print(f1.__dict__)
# print(dir(Foo.func))
# print(hasattr(Foo.func,'__set__'))  false
# print(hasattr(Foo.func,'__get__'))  True
# print(hasattr(Foo.func,'__delete__')) false
f1.func="hhh"       #定义了一个实例属性
# print(f1.func)
print(f1.__dict__) #{'func': 'hhh'}
# del f1.func         #删除了实例的属性
print(f1.__dict__)  #{}
print(f1.func)#<bound method Foo.func of <__main__.Foo object at 0x008FC410>
# f1.func()  #调用的是类的属性

猜你喜欢

转载自www.cnblogs.com/wuxi9864/p/9960372.html