對(duì)比Python中__getattr__和 __getattribute__獲取屬性的用法
相信大家覺得大多數(shù)時(shí)候我們并不太需要關(guān)注getattribute和getattr的一些細(xì)節(jié)(至少我自己吧:)),
一般情況下消費(fèi)我們自定義的類的時(shí)候,我們對(duì)類的結(jié)構(gòu)都了解,不會(huì)刻意偏離,造成一些屬性訪問的錯(cuò)誤。
不過作為一個(gè)有好奇心有追求有氣質(zhì)的python寶寶,怎么可能不稍稍研究一下呢。好吧,其實(shí)是在github上讀到一個(gè)開源項(xiàng)目sinaweibopy的源碼才看的,代碼挺有意思,正好當(dāng)作一個(gè)實(shí)用的例子,來看看如何自定義實(shí)現(xiàn)gettattr讓代碼更加的動(dòng)態(tài)優(yōu)雅:
# 例子在原來的基礎(chǔ)上簡(jiǎn)化了一下,排除依賴和干擾,詳細(xì)參見原項(xiàng)目
class UrlGenerator(object):
def __init__(self, root_url):
self.url = root_url
def __getattr__(self, item):
if item == 'get' or item == 'post':
print self.url
return UrlGenerator('{}/{}'.format(self.url, item))
url_gen = UrlGenerator('http://xxxx')
url_gen.users.show.get
>>> http://xxxx/users/show
充分利用getattr會(huì)在沒有查找到相應(yīng)實(shí)例屬性時(shí)被調(diào)用的特點(diǎn),方便的通過鏈?zhǔn)秸{(diào)用生成對(duì)應(yīng)的url,源代碼中在碰到http method的時(shí)候返回一個(gè)
可調(diào)用的對(duì)象更加的優(yōu)雅,鏈?zhǔn)降牟僮鞑粌H優(yōu)雅而且還能很好的說明調(diào)用的接口的意義(restful的接口啦)。
示例
1.__getattr__示例:
class Test(object):
def __init__(self,name):
self.name = name
def __getattr__(self, value):
if value == 'address':
return 'China'
if __name__=="__main__":
test = Test('letian')
print test.name
print test.address
test.address = 'Anhui'
print test.address
運(yùn)行結(jié)果:
letian China Anhui
如果是調(diào)用了一個(gè)類中未定義的方法,則__getattr__也要返回一個(gè)方法,例如:
class Test(object):
def __init__(self,name):
self.name = name
def __getattr__(self, value):
return len
if __name__=="__main__":
test = Test('letian')
print test.getlength('letian')
運(yùn)行結(jié)果:
6
2.__getattribute__示例:
class Test(object):
def __init__(self,name):
self.name = name
def __getattribute__(self, value):
if value == 'address':
return 'China'
if __name__=="__main__":
test = Test('letian')
print test.name
print test.address
test.address = 'Anhui'
print test.address
運(yùn)行結(jié)果:
None China China
深入思考
既然能通過定制類的getattr自定義方法來實(shí)現(xiàn)一些優(yōu)雅的功能,自然我們也要對(duì)它有一些了解,包括和它相似的自定義方法getattribute
1. 用作實(shí)例屬性的獲取和攔截
當(dāng)訪問某個(gè)實(shí)例屬性時(shí), getattribute會(huì)被無條件調(diào)用,如未實(shí)現(xiàn)自己的getattr方法,會(huì)拋出AttributeError提示找不到這個(gè)屬性,如果自定義了自己getattr方法的話,方法會(huì)在這種找不到屬性的情況下被調(diào)用,比如上面的例子中的情況。所以在找不到屬性的情況下通過實(shí)現(xiàn)自定義的getattr方法來實(shí)現(xiàn)一些功能是一個(gè)不錯(cuò)的方式,因?yàn)樗粫?huì)像getattribute方法每次都會(huì)調(diào)用可能會(huì)影響一些正常情況下的屬性訪問:
class Test(object):
def __init__(self, p):
self.p = p
def __getattr__(self, item):
return 'default'
t = Test('p1')
print t.p
print t.p2
>>> p1
>>> default
2. 自定義getattribute的時(shí)候防止無限遞歸
因?yàn)間etattribute在訪問屬性的時(shí)候一直會(huì)被調(diào)用,自定義的getattribute方法里面同時(shí)需要返回相應(yīng)的屬性,通過self.__dict__取值會(huì)繼續(xù)向下調(diào)用getattribute,造成循環(huán)調(diào)用:
class AboutAttr(object):
def __init__(self, name):
self.name = name
def __getattribute__(self, item):
try:
return super(AboutAttr, self).__getattribute__(item)
except KeyError:
return 'default'
這里通過調(diào)用綁定的super對(duì)象來獲取隊(duì)形的屬性,對(duì)新式類來說其實(shí)和object.__getattribute__(self, item)一樣的道理:
默認(rèn)情況下自定義的類會(huì)從object繼承g(shù)etattribute方法,對(duì)于屬性的查找是完全能用的
getattribute的實(shí)現(xiàn)感覺還是挺抽象化的,只需要綁定相應(yīng)的實(shí)例對(duì)象和要查找的屬性名稱就行
3.同時(shí)覆蓋掉getattribute和getattr的時(shí)候,在getattribute中需要模仿原本的行為拋出AttributeError或者手動(dòng)調(diào)用getattr
class AboutAttr(object):
def __init__(self, name):
self.name = name
def __getattribute__(self, item):
try:
return super(AboutAttr, self).__getattribute__(item)
except KeyError:
return 'default'
except AttributeError as ex:
print ex
def __getattr__(self, item):
return 'default'
at = AboutAttr('test')
print at.name
print at.not_exised
>>>test
>>>'AboutAttr' object has no attribute 'not_exised'
>>>None
上面例子里面的getattr方法根本不會(huì)被調(diào)用,因?yàn)樵镜腁ttributeError被我們自行處理并未拋出,也沒有手動(dòng)調(diào)用getattr,所以訪問not_existed的結(jié)果是None而不是default.
相關(guān)文章
使用python提取PowerPoint幻燈片中表格并保存到文本及Excel文件
owerPoint作為廣泛使用的演示工具,常被用于展示各類數(shù)據(jù)報(bào)告和分析結(jié)果,其中,表格以其直觀性和結(jié)構(gòu)性成為闡述數(shù)據(jù)關(guān)系的不二之選,本文將介紹如何使用Python來提取PowerPoint幻燈片中的表格,并將表格數(shù)據(jù)寫入文本文件以及Excel文件,需要的朋友可以參考下2024-06-06
python初學(xué)之用戶登錄的實(shí)現(xiàn)過程(實(shí)例講解)
下面小編就為大家分享一篇python初學(xué)之用戶登錄的實(shí)現(xiàn)過程(實(shí)例講解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
pytorch中Transformer進(jìn)行中英文翻譯訓(xùn)練的實(shí)現(xiàn)
本文主要介紹了pytorch中Transformer進(jìn)行中英文翻譯訓(xùn)練的實(shí)現(xiàn),詳細(xì)闡述了使用PyTorch實(shí)現(xiàn)Transformer模型的代碼實(shí)現(xiàn)和訓(xùn)練過程,具有一定參考價(jià)值,感興趣的可以了解一下2023-08-08
Python jieba 中文分詞與詞頻統(tǒng)計(jì)的操作
這篇文章主要介紹了Python jieba 中文分詞與詞頻統(tǒng)計(jì)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
From CSV to SQLite3 by python 導(dǎo)入csv到sqlite實(shí)例
今天小編就為大家分享一篇From CSV to SQLite3 by python 導(dǎo)入csv到sqlite實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02

