码迷,mamicode.com
首页 > 其他好文 > 详细

__get__,__set__,__delete__

时间:2018-11-14 22:42:53      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:col   ini   method   定义   调用   self   false   let   object   

# 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()  #调用的是类的属性

 

__get__,__set__,__delete__

标签:col   ini   method   定义   调用   self   false   let   object   

原文地址:https://www.cnblogs.com/wuxi9864/p/9960372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!