首頁技術(shù)文章正文

Python 黑魔法之描述符

更新時間:2018-08-23 來源:黑馬程序員人工智能+python培訓(xùn)學(xué)院 瀏覽量:

引言

Descriptors(描述符)是Python語言中一個深奧但很重要的一個黑魔法,它被廣泛應(yīng)用于Python語言的內(nèi)核,熟練掌握描述符將會為Python程序員的工具箱添加一個額外的技巧。本文我將講述描述符的定義以及一些常見的場景,并且在文末會補充一下__getattr,__getattribute__, __getitem__這三個同樣涉及到屬性訪問的魔術(shù)方法。

描述符的定義descr__get__(self, obj, objtype=None) --> valuedescr.__set__(self, obj, value) --> Nonedescr.__delete__(self, obj) --> None

只要一個object attribute(對象屬性)定義了上面三個方法中的任意一個,那么這個類就可以被稱為描述符類。

描述符基礎(chǔ)

下面這個例子中我們創(chuàng)建了一個RevealAcess類,并且實現(xiàn)了__get__方法,現(xiàn)在這個類可以被稱為一個描述符類。

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__方法的各個參數(shù)的含義,在下面這個例子中,self即RevealAccess類的實例x,obj即MyClass類的實例m,objtype顧名思義就是MyClass類自身。從輸出語句可以看出,m.x訪問描述符x會調(diào)用__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: <class '__main__.MyClass'>

EX2類屬性

如果通過類直接訪問屬性x,那么obj接直接為None,這還是比較好理解,因為不存在MyClass的實例。

>>> MyClass.xself in RevealAccess: <__main__.RevealAccess object at 0x7f53651070f0>self: <__main__.RevealAccess object at 0x7f53651070f0>obj: Noneobjtype: <class '__main__.MyClass'>描述符的原理描述符觸發(fā)

上面這個例子中,我們分別從實例屬性和類屬性的角度列舉了描述符的用法,下面我們來仔細分析一下內(nèi)部的原理:

如果是對實例屬性進行訪問,實際上調(diào)用了基類object的__getattribute__方法,在這個方法中將obj.d轉(zhuǎn)譯成了type(obj).__dict__['d'].__get__(obj, type(obj))。

如果是對類屬性進行訪問,相當(dāng)于調(diào)用了元類type的__getattribute__方法,它將cls.d轉(zhuǎn)譯成cls.__dict__['d'].__get__(None, cls),這里__get__()的obj為的None,因為不存在實例。

簡單講一下__getattribute__魔術(shù)方法,這個方法在我們訪問一個對象的屬性的時候會被無條件調(diào)用,詳細的細節(jié)比如和__getattr, __getitem__的區(qū)別我會在文章的末尾做一個額外的補充,我們暫時并不深究。

描述符優(yōu)先級

首先,描述符分為兩種:

如果一個對象同時定義了__get__()和__set__()方法,則這個描述符被稱為data descriptor。

如果一個對象只定義了__get__()方法,則這個描述符被稱為non-data descriptor。

我們對屬性進行訪問的時候存在下面四種情況:

data descriptor

instance dict

non-data descriptor

__getattr__()

它們的優(yōu)先級大小是:

data descriptor > instance dict > non-data descriptor > __getattr__()

這是什么意思呢?就是說如果實例對象obj中出現(xiàn)了同名的data descriptor->d 和 instance attribute->d,obj.d對屬性d進行訪問的時候,由于data descriptor具有更高的優(yōu)先級,Python便會調(diào)用type(obj).__dict__['d'].__get__(obj, type(obj))而不是調(diào)用obj.__dict__[‘d’]。但是如果描述符是個non-data descriptor,Python則會調(diào)用obj.__dict__['d']。

Property

每次使用描述符的時候都定義一個描述符類,這樣看起來非常繁瑣。Python提供了一種簡潔的方式用來向?qū)傩蕴砑訑?shù)據(jù)描述符。

property(fget=None, fset=None, fdel=None, doc=None) -> property attribute

fget、fset和fdel分別是類的getter、setter和deleter方法。我們通過下面的一個示例來說明如何使用Property:

class Account(object):    def __init__(self):        self._acct_num = None    def get_acct_num(self):        return self._acct_num    def set_acct_num(self, value):        self._acct_num = value    def del_acct_num(self):        del self._acct_num    acct_num = property(get_acct_num, set_acct_num, del_acct_num, '_acct_num property.')

如果acct是Account的一個實例,acct.acct_num將會調(diào)用getter,acct.acct_num = value將調(diào)用setter,del acct_num.acct_num將調(diào)用deleter。

>>> acct = Account()>>> acct.acct_num = 1000>>> acct.acct_num1000

Python也提供了@property裝飾器,對于簡單的應(yīng)用場景可以使用它來創(chuàng)建屬性。一個屬性對象擁有g(shù)etter,setter和deleter裝飾器方法,可以使用它們通過對應(yīng)的被裝飾函數(shù)的accessor函數(shù)創(chuàng)建屬性的拷貝。

class Account(object):    def __init__(self):        self._acct_num = None    @property     # the _acct_num property. the decorator creates a read-only property    def acct_num(self):        return self._acct_num    @acct_num.setter    # the _acct_num property setter makes the property writeable    def set_acct_num(self, value):        self._acct_num = value    @acct_num.deleter    def del_acct_num(self):        del self._acct_num

如果想讓屬性只讀,只需要去掉setter方法。

在運行時創(chuàng)建描述符

我們可以在運行時添加property屬性:

class Person(object):    def addProperty(self, attribute):        # create local setter and getter with a particular attribute name        getter = lambda self: self._getProperty(attribute)        setter = lambda self, value: self._setProperty(attribute, value)        # construct property attribute and add it to the class        setattr(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'}靜態(tài)方法和類方法

我們可以使用描述符來模擬Python中的@staticmethod和@classmethod的實現(xiàn)。我們首先來瀏覽一下下面這張表:

Transformation

Called from an Object

Called from a Class

function

f(obj, *args)

f(*args)

staticmethod

f(*args)

f(*args)

classmethod

f(type(obj), *args)

f(klass, *args)

靜態(tài)方法

對于靜態(tài)方法f。c.f和C.f是等價的,都是直接查詢object.__getattribute__(c, ‘f’)或者object.__getattribute__(C, ’f‘)。靜態(tài)方法一個明顯的特征就是沒有self變量。

靜態(tài)方法有什么用呢?假設(shè)有一個處理專門數(shù)據(jù)的容器類,它提供了一些方法來求平均數(shù),中位數(shù)等統(tǒng)計數(shù)據(jù)方式,這些方法都是要依賴于相應(yīng)的數(shù)據(jù)的。但是類中可能還有一些方法,并不依賴這些數(shù)據(jù),這個時候我們可以將這些方法聲明為靜態(tài)方法,同時這也可以提高代碼的可讀性。

使用非數(shù)據(jù)描述符來模擬一下靜態(tài)方法的實現(xiàn):

class StaticMethod(object):    def __init__(self, f):        self.f = f    def __get__(self, obj, objtype=None):        return self.f

我們來應(yīng)用一下:

class MyClass(object):    @StaticMethod    def get_x(x):        return xprint(MyClass.get_x(100))  # output: 100類方法

Python的@classmethod和@staticmethod的用法有些類似,但是還是有些不同,當(dāng)某些方法只需要得到類的引用而不關(guān)心類中的相應(yīng)的數(shù)據(jù)的時候就需要使用classmethod了。

使用非數(shù)據(jù)描述符來模擬一下類方法的實現(xiàn):

class ClassMethod(object):    def __init__(self, f):        self.f = f    def __get__(self, obj, klass=None):        if klass is None:            klass = type(obj)        def newfunc(*args):            return self.f(klass, *args)        return newfunc其他的魔術(shù)方法

首次接觸Python魔術(shù)方法的時候,我也被__get__, __getattribute__, __getattr__, __getitem__之間的區(qū)別困擾到了,它們都是和屬性訪問相關(guān)的魔術(shù)方法,其中重寫__getattr__,__getitem__來構(gòu)造一個自己的集合類非常的常用,下面我們就通過一些例子來看一下它們的應(yīng)用。

__getattr__

Python默認(rèn)訪問類/實例的某個屬性都是通過__getattribute__來調(diào)用的,__getattribute__會被無條件調(diào)用,沒有找到的話就會調(diào)用__getattr__。如果我們要定制某個類,通常情況下我們不應(yīng)該重寫__getattribute__,而是應(yīng)該重寫__getattr__,很少看見重寫__getattribute__的情況。

從下面的輸出可以看出,當(dāng)一個屬性通過__getattribute__無法找到的時候會調(diào)用__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__'應(yīng)用

對于默認(rèn)的字典,Python只支持以obj['foo']形式來訪問,不支持obj.foo的形式,我們可以通過重寫__getattr__讓字典也支持obj['foo']的訪問形式,這是一個非常經(jīng)典常用的用法:

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] = value    def __delattr__(self, key):        try:            del self[key]        except KeyError as k:            raise AttributeError(k)    def __repr__(self):        return '<Storage ' + dict.__repr__(self) + '>'

我們來使用一下我們自定義的加強版字典:

>>> s = Storage(a=1)>>> s['a']1>>> s.a1>>> s.a = 2>>> s['a']2>>> del s.a>>> s.a...AttributeError: 'a'__getitem__

getitem用于通過下標(biāo)[]的形式來獲取對象中的元素,下面我們通過重寫__getitem__來實現(xiàn)一個自己的list。

class MyList(object):    def __init__(self, *args):        self.numbers = args    def __getitem__(self, item):        return self.numbers[item]my_list = MyList(1, 2, 3, 4, 6, 5, 3)print my_list[2]

這個實現(xiàn)非常的簡陋,不支持slice和step等功能,請讀者自行改進,這里我就不重復(fù)了。

應(yīng)用

下面是參考requests庫中對于__getitem__的一個使用,我們定制了一個忽略屬性大小寫的字典類。

程序有些復(fù)雜,我稍微解釋一下:由于這里比較簡單,沒有使用描述符的需求,所以使用了@property裝飾器來代替,lower_keys的功能是將實例字典中的鍵全部轉(zhuǎn)換成小寫并且存儲在字典self._lower_keys中。重寫了__getitem__方法,以后我們訪問某個屬性首先會將鍵轉(zhuǎn)換為小寫的方式,然后并不會直接訪問實例字典,而是會訪問字典self._lower_keys去查找。賦值/刪除操作的時候由于實例字典會進行變更,為了保持self._lower_keys和實例字典同步,首先清除self._lower_keys的內(nèi)容,以后我們重新查找鍵的時候再調(diào)用__getitem__的時候會重新新建一個self._lower_keys。

class CaseInsensitiveDict(dict):    @property    def 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_keys    def _clear_lower_keys(self):        if hasattr(self, '_lower_keys'):            self._lower_keys.clear()    def __contains__(self, key):        return key.lower() in self.lower_keys    def __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

我們來調(diào)用一下這個類:

>>> 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培訓(xùn)學(xué)院
首發(fā):http://python.itheima.com/

分享到:
在線咨詢 我要報名
和我們在線交談!