Python黑魔法之描述符的使用介紹
引言
Descriptors(描述詞)是Python語言中一個深奧但很重要的一個黑魔法,它被廣泛應用於Python語言的內核,熟練描述符將會為Python程式設計師的工具箱添加一個額外的技巧。本文我將講述描述符的定義以及一些常見的場景,並且在文末會補充一下getattr
,getattribute
, getitem
這三個同樣涉及到屬性訪問的魔術方法。
描述子的定義
descrget(self, obj, objtype=None) --> value descr.set(self, obj, value) --> None descr.delete(self, obj) --> None
只要一個<a href="http://www.php.cn/wiki/60.html" target="_blank">object</a> 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.x self 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.x self in RevealAccess: <main.RevealAccess object at 0x7f53651070f0> self: <main.RevealAccess object at 0x7f53651070f0> obj: None objtype: <class 'main.MyClass'>
描述子的原理
描述子觸發
上面這個例子中,我們分別從實例屬性和類別屬性的角度列舉了描述符的用法,下面我們來仔細分析一下內部的原理:
如果是對
實例屬性
進行訪問,實際上呼叫了基底類別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 descriptor
- # #instance dict
- non-data descriptor
- #getattr()
data descriptor > instance dict > non-data descriptor > getattr()
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(fget=None, fset=None, fdel=None, doc=None) -> property attribute
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 = 1000 >>> acct.acct_num 1000
@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 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
在运行时创建描述符
我们可以在运行时添加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.name Getting: name 'John Smith' >>> user.dict {'_phone': '12345', '_name': 'John Smith'}
静态方法和类方法
我们可以使用描述符来模拟Python中的@<a href="http://www.php.cn/wiki/188.html" target="_blank">static</a>method
和@classmethod
的实现。我们首先来浏览一下下面这张表:
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) |
静态方法
对于静态方法f
。c.f
和C.f
是等价的,都是直接查询object.getattribute(c, ‘f’)
或者object.getattribute(C, ’f‘)
。静态方法一个明显的特征就是没有self
变量。
静态方法有什么用呢?假设有一个处理专门数据的容器类,它提供了一些方法来求平均数,中位数等统计数据方式,这些方法都是要依赖于相应的数据的。但是类中可能还有一些方法,并不依赖这些数据,这个时候我们可以将这些方法声明为静态方法,同时这也可以提高代码的可读性。
使用非数据描述符来模拟一下静态方法的实现:
class StaticMethod(object): def init(self, f): self.f = f def get(self, obj, objtype=None): return self.f
我们来应用一下:
class MyClass(object): @StaticMethod def get_x(x): return x print(MyClass.get_x(100)) # output: 100
类方法
Python的@classmethod
和@staticmethod
的用法有些类似,但是还是有些不同,当某些方法只需要得到类的<a href="http://www.php.cn/wiki/231.html" target="_blank">引用</a>
而不关心类中的相应的数据的时候就需要使用classmethod了。
使用非数据描述符来模拟一下类方法的实现:
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
其他的魔术方法
首次接触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().a call 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] = 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.a 1 >>> 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 = args def 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): @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
我们来调用一下这个类:
>>> 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']) ziwenxi
以上是Python黑魔法之描述符的使用介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

在 Sublime Text 中運行 Python 代碼,需先安裝 Python 插件,再創建 .py 文件並編寫代碼,最後按 Ctrl B 運行代碼,輸出會在控制台中顯示。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。

在 Notepad 中運行 Python 代碼需要安裝 Python 可執行文件和 NppExec 插件。安裝 Python 並為其添加 PATH 後,在 NppExec 插件中配置命令為“python”、參數為“{CURRENT_DIRECTORY}{FILE_NAME}”,即可在 Notepad 中通過快捷鍵“F6”運行 Python 代碼。
