對比Python中__getattr__和 __getattribute__擷取屬性的用法

來源:互聯網
上載者:User
相信大家覺得大多數時候我們並不太需要關注getattribute和getattr的一些細節(至少我自己吧:)),
一般情況下消費我們自訂的類的時候,我們對類的結構都瞭解,不會刻意偏離,造成一些屬性訪問的錯誤。

不過作為一個有好奇心有追求有氣質的python寶寶,怎麼可能不稍稍研究一下呢。好吧,其實是在github上讀到一個開源項目sinaweibopy的源碼才看的,代碼挺有意思,正好當作一個實用的例子,來看看如何自訂實現gettattr讓代碼更加的動態優雅:

# 例子在原來的基礎上簡化了一下,排除依賴和幹擾,詳細參見原項目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的時候返回一個
可調用的對象更加的優雅,鏈式的操作不僅優雅而且還能很好的說明調用的介面的意義(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

運行結果:

letianChinaAnhui

如果是調用了一個類中未定義的方法,則__getattr__也要返回一個方法,例如:

class Test(object):  def __init__(self,name):    self.name = name  def __getattr__(self, value):    return lenif __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

運行結果:

NoneChinaChina

深入思考
既然能通過定製類的getattr自訂方法來實現一些優雅的功能,自然我們也要對它有一些瞭解,包括和它相似的自訂方法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.pprint t.p2>>> p1>>> default

2. 自訂getattribute的時候防止無限遞迴
因為getattribute在訪問屬性的時候一直會被調用,自訂的getattribute方法裡面同時需要返回相應的屬性,通過self.__dict__取值會繼續向下調用getattribute,造成迴圈調用:

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.nameprint at.not_exised>>>test>>>'AboutAttr' object has no attribute 'not_exised'>>>None

上面例子裡面的getattr方法根本不會被調用,因為原本的AttributeError被我們自行處理並未拋出,也沒有手動調用getattr,所以訪問not_existed的結果是None而不是default.

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.