__get__,__set__,__delete__
阿新 • • 發佈:2018-11-14
# 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() #呼叫的是類的屬性