Python描述符常用情境詳解

來源:互聯網
上載者:User

標籤:

Descriptors( 描述符 ) 是語言中一個深奧但很重要的一個黑魔法,它被廣泛應用於 Python 語言的核心,熟練掌握描述符將會為 Python程式員 的工具箱添加一個額外的技巧。本文將講述描述符的定義以及一些常見的情境,並且在文末會補充一下 __getattr , __getattribute__, __getitem__ 這三個同樣涉及到屬性訪問的魔術方法,希望對大家 學習python有所協助。 描述符的定義descr__get__(self, obj, objtype=None) --> valuedescr.__set__(self, obj, value) --> Nonedescr.__delete__(self, obj) --> None只要一個object attribute( 對象屬性 ) 定義了上面三個方法中的任意一個,那麼這個類就可以被稱為描述符類。 描述符基礎下面這個例子中我們建立了一個RevealAcess 類,並且實現了 __get__ 方法,現在這個類可以被稱為一個描述符類。class RevealAccess(object):def __get__(self, obj, objtype):print(’self in RevealAccess: {}’.format(self))print(’self: {}\nobj: {}\nobjtype: {}’.format(self, obj, objtype))class MyClass(object):x = RevealAccess()def test(self):print(’self in MyClass: {}’.format(self)) EX1執行個體屬性接下來我們來看一下__get__ 方法的各個參數的含義,在下面這個例子中, self 即 RevealAccess 類的執行個體 x , obj 即 MyClass 類的執行個體 m , objtype 顧名思義就是 MyClass 類自身。從輸出語句可以看出, m.x訪問描述符 x 會調用 __get__ 方法。>>> m = MyClass()>>> m.test()self in MyClass:<__main__.myclass object="" at="" 0x7f19d4e42160="">>>> m.xself in RevealAccess:<__main__.revealaccess object="" at="" 0x7f19d4e420f0="">self:<__main__.revealaccess object="" at="" 0x7f19d4e420f0="">obj:<__main__.myclass object="" at="" 0x7f19d4e42160="">objtype: EX2類屬性如果通過類直接存取屬性x ,那麼 obj 接直接為 None ,這還是比較好理解,因為不存在 MyClass 的執行個體。>>> MyClass.xself in RevealAccess:<__main__.revealaccess object="" at="" 0x7f53651070f0="">self:<__main__.revealaccess object="" at="" 0x7f53651070f0="">obj: Noneobjtype: 描述符的原理 描述符觸發上面這個例子中,我們分別從執行個體屬性和類屬性的角度列舉了描述符的用法,下面我們來仔細分析一下內部的原理:如果是對執行個體屬性進行訪問,實際上調用了基類object 的 __getattribute__ 方法,在這個方法中將 obj.d轉譯成了 type(obj).__dict__[’d’].__get__(obj, type(obj)) 。如果是對類屬性進行訪問,相當於調用了元類type 的 __getattribute__ 方法,它將 cls.d 轉譯成cls.__dict__[’d’].__get__(None, cls) ,這裡 __get__() 的 obj 為的 None ,因為不存在執行個體。簡單講一下__getattribute__ 魔術方法,這個方法在我們訪問一個對象的屬性的時候會被無條件調用,詳細的細節比如和 __getattr, __getitem__ 的區別我會在的末尾做一個額外的補充,我們暫時並不深究。 描述符優先順序首先,描述符分為兩種:如果一個對象同時定義了__get__() 和 __set__() 方法,則這個描述符被稱為 data descriptor 。如果一個對象只定義了__get__() 方法,則這個描述符被稱為 non-data descriptor 。我們對屬性進行訪問的時候存在下面四種情況:data descriptorinstance dictnon-data descriptor__getattr__()它們的優先順序大小是:data descriptor > instance dict > non-data descriptor > __getattr__()這是什麼意思呢?就是說如果執行個體對象obj 中出現了同名的 data descriptor->d  和  instance attribute->d, obj.d 對屬性 d 進行訪問的時候,由於 data descriptor 具有更高的優先順序, Python 便會調用type(obj).__dict__[’d’].__get__(obj, type(obj)) 而不是調用 obj.__dict__[‘d’] 。但是如果描述符是個 non-data descriptor , Python 則會調用 obj.__dict__[’d’] 。 Property每次使用描述符的時候都定義一個描述符類,這樣看起來非常繁瑣。Python 提供了一種簡潔的方式用來向屬性添加資料描述符。property(fget=None, fset=None, fdel=None, doc=None) -> property attributefget 、 fset 和 fdel 分別是類的 getter 、 setter 和 deleter 方法。我們通過下面的一個樣本來說明如何使用 Property :class Account(object):def __init__(self):self._acct_num = Nonedef get_acct_num(self):return self._acct_numdef set_acct_num(self, value):self._acct_num = valuedef del_acct_num(self):del self._acct_numacct_num = property(get_acct_num, set_acct_num, del_acct_num, ’_acct_num property.’)如果acct 是 Account 的一個執行個體, acct.acct_num 將會調用 getter , acct.acct_num = value 將調用setter , del acct_num.acct_num 將調用 deleter 。>>> acct = Account()>>> acct.acct_num = 1000>>> acct.acct_num1000Python 也提供了 @property 裝飾器,對於簡單的應用情境可以使用它來建立屬性。一個屬性對象擁有getter,setter 和 deleter 裝飾器方法,可以使用它們通過對應的被裝飾函數的 accessor 函數建立屬性的拷貝。class Account(object):def __init__(self):self._acct_num = None@property# the _acct_num property. the decorator creates a read-only propertydef acct_num(self):return self._acct_num@acct_num.setter# the _acct_num property setter makes the property writeabledef set_acct_num(self, value):self._acct_num = value@acct_num.deleterdef del_acct_num(self):del self._acct_num如果想讓屬性唯讀,只需要去掉setter 方法。 在運行時建立描述符我們可以在運行時添加property 屬性:class Person(object):def addProperty(self, attribute):# create local setter and getter with a particular attribute namegetter = lambda self: self._getProperty(attribute)setter = lambda self, value: self._setProperty(attribute, value)# construct property attribute and add it to the classsetattr(self.__class__, attribute, property(fget=getter, fset=setter, doc="Auto-generated method"))def _setProperty(self, attribute, value):print("Setting: {} = {}".format(attribute, value))setattr(self, ’_’ + attribute, value.title())def _getProperty(self, attribute):print("Getting: {}".format(attribute))return getattr(self, ’_’ + attribute)>>> user = Person()>>> user.addProperty(’name’)>>> user.addProperty(’phone’)>>> user.name = ’john smith’Setting: name = john smith>>> user.phone = ’12345’Setting: phone = 12345>>> user.nameGetting: name’John Smith’>>> user.__dict__{’_phone’: ’12345’, ’_name’: ’John Smith’} 靜態方法和類方法我們可以使用描述符來類比Python 中的 @staticmethod 和 @classmethod 的實現。我們首先來瀏覽一下下面這張表: 靜態方法對於靜態方法f 。 c.f 和 C.f 是等價的,都是直接查詢 object.__getattribute__(c, ‘f’) 或者object.__getattribute__(C, ’f‘) 。靜態方法一個明顯的特徵就是沒有 self 變數。靜態方法有什麼用呢?假設有一個處理專門資料的容器類,它提供了一些方法來求平均數,中位元等統計資料方式,這些方法都是要依賴於相應的資料的。但是類中可能還有一些方法,並不依賴這些資料,這個時候我們可以將這些方法聲明為靜態方法,同時這也可以提高代碼的可讀性。使用非資料描述符來類比一下靜態方法的實現:class StaticMethod(object):def __init__(self, f):self.f = fdef __get__(self, obj, objtype=None):return self.f我們來應用一下:class MyClass(object):@StaticMethoddef get_x(x):return xprint(MyClass.get_x(100))  # output: 100 類方法Python 的 @classmethod 和 @staticmethod 的用法有些類似,但是還是有些不同,當某些方法只需要得到類的引用而不關心類中的相應的資料的時候就需要使用 classmethod 了。使用非資料描述符來類比一下類方法的實現:class ClassMethod(object):def __init__(self, f):self.f = fdef __get__(self, obj, klass=None):if klass is None:klass = type(obj)def newfunc(*args):return self.f(klass, *args)return newfunc 其他的魔術方法首次接觸Python 魔術方法的時候,我也被 __get__, __getattribute__, __getattr__, __getitem__ 之間的區別困擾到了,它們都是和屬性訪問相關的魔術方法,其中重寫 __getattr__ , __getitem__ 來構造一個自己的集合類非常的常用,下面我們就通過一些例子來看一下它們的應用。__getattr__Python 預設訪問類 / 執行個體的某個屬性都是通過 __getattribute__ 來調用的, __getattribute__ 會被無條件調用,沒有找到的話就會調用 __getattr__ 。如果我們要定製某個類,通常情況下我們不應該重寫__getattribute__ ,而是應該重寫 __getattr__ ,很少看見重寫 __getattribute__ 的情況。從下面的輸出可以看出,當一個屬性通過__getattribute__ 無法找到的時候會調用 __getattr__ 。In [1]: class Test(object):...:  def __getattribute__(self, item):...:  print(’call __getattribute__’)...:  return super(Test, self).__getattribute__(item)...:  def __getattr__(self, item):...:  return ’call __getattr__’...:In [2]: Test().acall __getattribute__Out[2]: ’call __getattr__’ 應用對於預設的字典,Python 只支援以 obj[’foo’] 形式來訪問,不支援 obj.foo 的形式,我們可以通過重寫__getattr__ 讓字典也支援 obj[’foo’] 的訪問形式,這是一個非常經典常用的用法:class Storage(dict):"""  A Storage object is like a dictionary except `obj.foo` can be used  in addition to `obj[’foo’]`.  """def __getattr__(self, key):try:return self[key]except KeyError as k:raise AttributeError(k)def __setattr__(self, key, value):self[key] = valuedef __delattr__(self, key):try:del self[key]except KeyError as k:raise AttributeError(k)def __repr__(self):return ’’!我們來使用一下我們自訂的加強版字典:>>> s = Storage(a=1)>>> s[’a’]1>>> s.a1>>> s.a = 2>>> s[’a’]2>>> del s.a>>> s.a...AttributeError: ’a’__getitem__getitem 用於通過下標 [] 的形式來擷取對象中的元素,下面我們通過重寫 __getitem__ 來實現一個自己的 list 。class MyList(object):def __init__(self, *args):self.numbers = argsdef __getitem__(self, item):return self.numbers[item]my_list = MyList(1, 2, 3, 4, 6, 5, 3)print my_list[2]這個實現非常的簡陋,不支援slice 和 step 等功能,請讀者自行改進,這裡我就不重複了。 應用下面是參考requests 庫中對於 __getitem__ 的一個使用,我們定製了一個忽略屬性大小寫字典類。程式有些複雜,我稍微解釋一下:由於這裡比較簡單,沒有使用描述符的需求,所以使用了@property裝飾器來代替, lower_keys 的功能是將執行個體字典中的鍵全部轉換成小寫並且儲存在字典 self._lower_keys中。重寫了 __getitem__ 方法,以後我們訪問某個屬性首先會將鍵轉換為小寫方式,然後並不會直接存取執行個體字典,而是會訪問字典 self._lower_keys 去尋找。賦值 / 刪除操作的時候由於執行個體字典會進行變更,為了保持 self._lower_keys 和執行個體字典同步,首先清除 self._lower_keys 的內容,以後我們重新尋找鍵的時候再調用 __getitem__ 的時候會重新建立一個 self._lower_keys 。class CaseInsensitiveDict(dict):@propertydef lower_keys(self):if not hasattr(self, ’_lower_keys’) or not self._lower_keys:self._lower_keys = dict((k.lower(), k) for k in self.keys())return self._lower_keysdef _clear_lower_keys(self):if hasattr(self, ’_lower_keys’):self._lower_keys.clear()def __contains__(self, key):return key.lower() in self.lower_keysdef __getitem__(self, key):if key in self:return dict.__getitem__(self, self.lower_keys[key.lower()])def __setitem__(self, key, value):dict.__setitem__(self, key, value)self._clear_lower_keys()def __delitem__(self, key):dict.__delitem__(self, key)self._lower_keys.clear()def get(self, key, default=None):if key in self:return self[key]else:return default我們來調用一下這個類:>>> d = CaseInsensitiveDict()>>> d[’ziwenxie’] = ’ziwenxie’>>> d[’ZiWenXie’] = ’ZiWenXie’>>> print(d){’ZiWenXie’: ’ziwenxie’, ’ziwenxie’: ’ziwenxie’}>>> print(d[’ziwenxie’])ziwenxie# d[’ZiWenXie’] => d[’ziwenxie’]>>> print(d[’ZiWenXie’])ziwenxie來源: 軟體測試混混的部落格

Python描述符常用情境詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.