Python如何將裝飾器定義為類
問題
你想使用一個(gè)裝飾器去包裝函數(shù),但是希望返回一個(gè)可調(diào)用的實(shí)例。 你需要讓你的裝飾器可以同時(shí)工作在類定義的內(nèi)部和外部。
解決方案
為了將裝飾器定義成一個(gè)實(shí)例,你需要確保它實(shí)現(xiàn)了 __call__() 和 __get__() 方法。 例如,下面的代碼定義了一個(gè)類,它在其他函數(shù)上放置一個(gè)簡(jiǎn)單的記錄層:
import typesfrom functools import wrapsclass Profiled: def __init__(self, func): wraps(func)(self) self.ncalls = 0 def __call__(self, *args, **kwargs): self.ncalls += 1 return self.__wrapped__(*args, **kwargs) def __get__(self, instance, cls): if instance is None: return self else: return types.MethodType(self, instance)
你可以將它當(dāng)做一個(gè)普通的裝飾器來(lái)使用,在類里面或外面都可以:
@Profileddef add(x, y): return x + yclass Spam: @Profiled def bar(self, x): print(self, x)
在交互環(huán)境中的使用示例:
>>> add(2, 3)5>>> add(4, 5)9>>> add.ncalls2>>> s = Spam()>>> s.bar(1)<__main__.Spam object at 0x10069e9d0> 1>>> s.bar(2)<__main__.Spam object at 0x10069e9d0> 2>>> s.bar(3)<__main__.Spam object at 0x10069e9d0> 3>>> Spam.bar.ncalls3
討論
將裝飾器定義成類通常是很簡(jiǎn)單的。但是這里還是有一些細(xì)節(jié)需要解釋下,特別是當(dāng)你想將它作用在實(shí)例方法上的時(shí)候。
首先,使用 functools.wraps() 函數(shù)的作用跟之前還是一樣,將被包裝函數(shù)的元信息復(fù)制到可調(diào)用實(shí)例中去。
其次,通常很容易會(huì)忽視上面的 __get__() 方法。如果你忽略它,保持其他代碼不變?cè)俅芜\(yùn)行, 你會(huì)發(fā)現(xiàn)當(dāng)你去調(diào)用被裝飾實(shí)例方法時(shí)出現(xiàn)很奇怪的問題。例如:
>>> s = Spam()>>> s.bar(3)Traceback (most recent call last):...TypeError: bar() missing 1 required positional argument: ’x’
出錯(cuò)原因是當(dāng)方法函數(shù)在一個(gè)類中被查找時(shí),它們的 __get__() 方法依據(jù)描述器協(xié)議被調(diào)用, 在8.9小節(jié)已經(jīng)講述過描述器協(xié)議了。在這里,__get__() 的目的是創(chuàng)建一個(gè)綁定方法對(duì)象 (最終會(huì)給這個(gè)方法傳遞self參數(shù))。下面是一個(gè)例子來(lái)演示底層原理:
>>> s = Spam()>>> def grok(self, x):... pass...>>> grok.__get__(s, Spam)<bound method Spam.grok of <__main__.Spam object at 0x100671e90>>>>>
__get__() 方法是為了確保綁定方法對(duì)象能被正確的創(chuàng)建。 type.MethodType() 手動(dòng)創(chuàng)建一個(gè)綁定方法來(lái)使用。只有當(dāng)實(shí)例被使用的時(shí)候綁定方法才會(huì)被創(chuàng)建。 如果這個(gè)方法是在類上面來(lái)訪問, 那么 __get__() 中的instance參數(shù)會(huì)被設(shè)置成None并直接返回 Profiled 實(shí)例本身。 這樣的話我們就可以提取它的 ncalls 屬性了。
如果你想避免一些混亂,也可以考慮另外一個(gè)使用閉包和 nonlocal 變量實(shí)現(xiàn)的裝飾器,這個(gè)在9.5小節(jié)有講到。例如:
import typesfrom functools import wrapsdef profiled(func): ncalls = 0 @wraps(func) def wrapper(*args, **kwargs): nonlocal ncalls ncalls += 1 return func(*args, **kwargs) wrapper.ncalls = lambda: ncalls return wrapper# Example@profileddef add(x, y): return x + y
這個(gè)方式跟之前的效果幾乎一樣,除了對(duì)于 ncalls 的訪問現(xiàn)在是通過一個(gè)被綁定為屬性的函數(shù)來(lái)實(shí)現(xiàn),例如:
>>> add(2, 3)5>>> add(4, 5)9>>> add.ncalls()2>>>
以上就是Python如何將裝飾器定義為類的詳細(xì)內(nèi)容,更多關(guān)于Python將裝飾器定義為類的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Android table布局開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器2. IntelliJ IDEA安裝插件的方法步驟3. 理解PHP5中static和const關(guān)鍵字4. php模擬實(shí)現(xiàn)斗地主發(fā)牌5. spring acegi security 1.0.0 發(fā)布6. MyBatis中的JdbcType映射使用詳解7. vue 使用localstorage實(shí)現(xiàn)面包屑的操作8. Python random庫(kù)使用方法及異常處理方案9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備