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ì)思想。值得一提的是,在重寫(xiě)__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 pandas plot輸出圖形中顯示中文亂碼問(wèn)題
今天小編就為大家分享一篇解決Python pandas plot輸出圖形中顯示中文亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python入門(mén)教程(七)Python數(shù)字類型
這篇文章主要介紹了Python入門(mén)教程(七)Python數(shù)字類型,Python是一門(mén)非常強(qiáng)大好用的語(yǔ)言,也有著易上手的特性,本文為入門(mén)教程,需要的朋友可以參考下2023-04-04
從基礎(chǔ)到高級(jí)詳解DataFrame中時(shí)間序列操作的核心方法
時(shí)間序列數(shù)據(jù)是數(shù)據(jù)分析中最常見(jiàn)的類型之一,本文將系統(tǒng)介紹DataFrame中時(shí)間序列操作的核心方法,希望可以幫助大家掌握時(shí)間數(shù)據(jù)處理的全流程2026-02-02
Python使用Tkinter編寫(xiě)串口通信調(diào)試工具
串口調(diào)試助手是一款基于Python和Tkinter開(kāi)發(fā)的串口通信調(diào)試工具, 專為工程師和開(kāi)發(fā)者設(shè)計(jì),下面為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)串口通信調(diào)試工具,希望對(duì)大家有所幫助2026-01-01

