python五十二:__setattr__,__delattr__,__getattr__方法
生活随笔
收集整理的這篇文章主要介紹了
python五十二:__setattr__,__delattr__,__getattr__方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
class Foo:def __init__(self,x):self.x = xdef __getattr__(self, item):print("執(zhí)行了getattr方法")def __delattr__(self, item):print("執(zhí)行了刪除方法",item)def __setattr__(self, key, value):# self.key = valueprint("執(zhí)行了__setattr__方法")self.__dict__[key] = value # 加屬性的本質(zhì)就是往__dict__字典中加值f = Foo(666)
print(f.x)
print(getattr(f,'x'))
print(f.yyy) # 當調(diào)用了對象中不存在的屬性時,會調(diào)用對象的__getattr()方法del f.x # 當調(diào)用del,會觸發(fā)__delattr__方法f.y = 2
print(f.__dict__)
?
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的python五十二:__setattr__,__delattr__,__getattr__方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汇编:中断过程
- 下一篇: websocket python爬虫_p