對比Python中__getattr__和 __getattribute__獲取屬性的用法
相信大家覺得大多數時候我們并不太需要關注getattribute和getattr的一些細節(jié)(至少我自己吧:)),
一般情況下消費我們自定義的類的時候,我們對類的結構都了解,不會刻意偏離,造成一些屬性訪問的錯誤。
不過作為一個有好奇心有追求有氣質的python寶寶,怎么可能不稍稍研究一下呢。好吧,其實是在github上讀到一個開源項目sinaweibopy的源碼才看的,代碼挺有意思,正好當作一個實用的例子,來看看如何自定義實現gettattr讓代碼更加的動態(tài)優(yōu)雅:
# 例子在原來的基礎上簡化了一下,排除依賴和干擾,詳細參見原項目
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會在沒有查找到相應實例屬性時被調用的特點,方便的通過鏈式調用生成對應的url,源代碼中在碰到http method的時候返回一個
可調用的對象更加的優(yōu)雅,鏈式的操作不僅優(yōu)雅而且還能很好的說明調用的接口的意義(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
運行結果:
letian China Anhui
如果是調用了一個類中未定義的方法,則__getattr__也要返回一個方法,例如:
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')
運行結果:
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
運行結果:
None China China
深入思考
既然能通過定制類的getattr自定義方法來實現一些優(yōu)雅的功能,自然我們也要對它有一些了解,包括和它相似的自定義方法getattribute
1. 用作實例屬性的獲取和攔截
當訪問某個實例屬性時, getattribute會被無條件調用,如未實現自己的getattr方法,會拋出AttributeError提示找不到這個屬性,如果自定義了自己getattr方法的話,方法會在這種找不到屬性的情況下被調用,比如上面的例子中的情況。所以在找不到屬性的情況下通過實現自定義的getattr方法來實現一些功能是一個不錯的方式,因為它不會像getattribute方法每次都會調用可能會影響一些正常情況下的屬性訪問:
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的時候防止無限遞歸
因為getattribute在訪問屬性的時候一直會被調用,自定義的getattribute方法里面同時需要返回相應的屬性,通過self.__dict__取值會繼續(xù)向下調用getattribute,造成循環(huán)調用:
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'
這里通過調用綁定的super對象來獲取隊形的屬性,對新式類來說其實和object.__getattribute__(self, item)一樣的道理:
默認情況下自定義的類會從object繼承getattribute方法,對于屬性的查找是完全能用的
getattribute的實現感覺還是挺抽象化的,只需要綁定相應的實例對象和要查找的屬性名稱就行
3.同時覆蓋掉getattribute和getattr的時候,在getattribute中需要模仿原本的行為拋出AttributeError或者手動調用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方法根本不會被調用,因為原本的AttributeError被我們自行處理并未拋出,也沒有手動調用getattr,所以訪問not_existed的結果是None而不是default.
相關文章
Python+PyQt5實現開發(fā)Memcached客戶端
這篇文章主要介紹了如何使用Python和PyQt5來制作一個Memcached客戶端,以便我們可以輕松地與Memcached服務器進行交互,感興趣的小伙伴可以了解一下2023-06-06

