python中的getattribute 、getattr、setattr方法詳解
一、__getattribute__()
顧名思義,當(dāng)訪問object的屬性會調(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}')
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)我們通過object.attrname的形式訪問實例屬性時,實際上我們是通過__getattribute__得到了該屬性,是不是聯(lián)想到了OOP中封裝的思想?別急,下面會看到更多的oop設(shè)計思想。值得一提的是,在重寫__getattribute__()方法時,一定要知道你在做什么,否則可能導(dǎo)致無法正確訪問實例對象。另外,官方文檔建議始終使用基類方法來設(shè)置屬性,否則會陷入無限遞歸,最終棧溢出:

比如可以嘗試:
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
究其原因,是因為當(dāng)使用self.name訪問屬性時會調(diào)用__getattribute__,而__getattribute__又要訪問self.name,因此會無限遞歸下去。正確的做法是使用基類方法,對于該例子由于繼承的是基類,因此使用super().__getattribute__(attr)或object.__getattribute__(self,attr)均可。
二、__setattr__()
實例初始化過程中,為實例屬性賦值時會調(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__中也盡量使用基類的該方法來設(shè)置一些屬性,否則可能發(fā)生無限遞歸。
三、__getattr__()
從字面意思理解跟__getattribute__差不多,事實上,該方法是__getattribute__的補充,當(dāng)訪問某些屬性不存在,或**__getattribute__顯示地拋出AttributeError**,會自動轉(zhuǎn)到該方法做進一步處理。

可以測試:
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
測試通過主動拋出異常的方式觸發(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中的 sort 和 sorted的用法與區(qū)別
這篇文章主要介紹了Python中的 sort 和 sorted的用法與區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
python3 map函數(shù)和filter函數(shù)詳解
這篇文章主要介紹了python3 map函數(shù)和filter函數(shù)詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
Python使用thread模塊實現(xiàn)多線程的操作
線程(Threads)是操作系統(tǒng)提供的一種輕量級的執(zhí)行單元,可以在一個進程內(nèi)并發(fā)執(zhí)行多個任務(wù),每個線程都有自己的執(zhí)行上下文,包括棧、寄存器和程序計數(shù)器,本文給大家介紹了Python使用thread模塊實現(xiàn)多線程的操作,需要的朋友可以參考下2024-10-10

