python中的getattribute 、getattr、setattr方法詳解
一、__getattribute__()
顧名思義,當(dāng)訪問(wèn)object的屬性會(huì)調(diào)用該方法,可以測(cè)試:
class A(object): def __init__(self, name,age): self.name = name self.age = age def __getattribute__(self, attr): print("__getattribute__ is called") try: return super().__getattribute__(attr) except AttributeError: print(f'have no attr of {attr}') if __name__ == '__main__': a = A('jyz',200) print(a.name) print(a.age) print(a.gender)
輸出:
__getattribute__ is called
jyz
__getattribute__ is called
200
__getattribute__ is called
have no attr of gender
None
可以看出,當(dāng)我們通過(guò)object.attrname的形式訪問(wèn)實(shí)例屬性時(shí),實(shí)際上我們是通過(guò)__getattribute__得到了該屬性,是不是聯(lián)想到了OOP中封裝的思想?別急,下面會(huì)看到更多的oop設(shè)計(jì)思想。值得一提的是,在重寫__getattribute__()方法時(shí),一定要知道你在做什么,否則可能導(dǎo)致無(wú)法正確訪問(wèn)實(shí)例對(duì)象。另外,官方文檔建議始終使用基類方法來(lái)設(shè)置屬性,否則會(huì)陷入無(wú)限遞歸,最終棧溢出:
比如可以嘗試:
class A(object): def __init__(self, name,age): self.name = name self.age = age def __getattribute__(self, attr): return self.name if __name__ == '__main__': a = A('jyz',200) print(a.name)
輸出:
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
究其原因,是因?yàn)楫?dāng)使用self.name訪問(wèn)屬性時(shí)會(huì)調(diào)用__getattribute__,而__getattribute__又要訪問(wèn)self.name,因此會(huì)無(wú)限遞歸下去。正確的做法是使用基類方法,對(duì)于該例子由于繼承的是基類,因此使用super().__getattribute__(attr)或object.__getattribute__(self,attr)均可。
二、__setattr__()
實(shí)例初始化過(guò)程中,為實(shí)例屬性賦值時(shí)會(huì)調(diào)用該方法。
class A(object): def __init__(self, name,age): self.name = name self.age = age def __getattribute__(self, attr): print("__getattribute__ is called") try: return super().__getattribute__(attr) except AttributeError: print(f'have no attr of {attr}') def __setattr__(self, key, value): print(f"__setattr__() is called, key is {key}") object.__setattr__(self, key, value)
輸出:
__setattr__() is called, key is name
__setattr__() is called, key is age
與__getattribute__同理,在__setattr__中也盡量使用基類的該方法來(lái)設(shè)置一些屬性,否則可能發(fā)生無(wú)限遞歸。
三、__getattr__()
從字面意思理解跟__getattribute__差不多,事實(shí)上,該方法是__getattribute__的補(bǔ)充,當(dāng)訪問(wèn)某些屬性不存在,或**__getattribute__顯示地拋出AttributeError**,會(huì)自動(dòng)轉(zhuǎn)到該方法做進(jìn)一步處理。
可以測(cè)試:
class A(object): def __init__(self, name,age): self.name = name self.age = age def __getattr__(self, attr): print(f"__getattr__() is called,but {attr} is not exist!") if __name__ == '__main__': a = A('jyz',200) print(a.gender)
輸出:
__getattr__() is called,but gender is not exist!
None
測(cè)試通過(guò)主動(dòng)拋出異常的方式觸發(fā)__getattr__():
class A(object): def __init__(self, name,age): self.name = name self.age = age def __getattribute__(self, attr): if attr not in ['name','age']: raise AttributeError else: return object.__getattribute__(self, attr) def __getattr__(self, attr): print(f"__getattr__() is called,but {attr} is not exist!") if __name__ == '__main__': a = A('jyz',200) print(a.name) print(a.gender)
輸出:
jyz
__getattr__() is called,but gender is not exist!
None
可以看到,上面兩種方式都可以觸發(fā),__getattr__()。
到此這篇關(guān)于python中的getattribute 、getattr、setattr方法詳解的文章就介紹到這了,更多相關(guān)getattribute 、getattr、setattr方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中的 sort 和 sorted的用法與區(qū)別
這篇文章主要介紹了Python中的 sort 和 sorted的用法與區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08python3 map函數(shù)和filter函數(shù)詳解
這篇文章主要介紹了python3 map函數(shù)和filter函數(shù)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Python使用thread模塊實(shí)現(xiàn)多線程的操作
線程(Threads)是操作系統(tǒng)提供的一種輕量級(jí)的執(zhí)行單元,可以在一個(gè)進(jìn)程內(nèi)并發(fā)執(zhí)行多個(gè)任務(wù),每個(gè)線程都有自己的執(zhí)行上下文,包括棧、寄存器和程序計(jì)數(shù)器,本文給大家介紹了Python使用thread模塊實(shí)現(xiàn)多線程的操作,需要的朋友可以參考下2024-10-10十行Python代碼實(shí)現(xiàn)文字識(shí)別功能
這篇文章主要和大家分享如何調(diào)用百度的接口實(shí)現(xiàn)圖片的文字識(shí)別。整體是用Python實(shí)現(xiàn),所需要使用的第三方庫(kù)包括aip、PIL、keyboard、pyinstaller,需要的可以參考一下2022-05-05