W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
你想使用一個裝飾器去包裝函數(shù),但是希望返回一個可調(diào)用的實(shí)例。你需要讓你的裝飾器可以同時工作在類定義的內(nèi)部和外部。
為了將裝飾器定義成一個實(shí)例,你需要確保它實(shí)現(xiàn)了 __call__()
和 __get__()
方法。例如,下面的代碼定義了一個類,它在其他函數(shù)上放置一個簡單的記錄層:
import types
from functools import wraps
class 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)做一個普通的裝飾器來使用,在類里面或外面都可以:
@Profiled
def add(x, y):
return x + y
class Spam:
@Profiled
def bar(self, x):
print(self, x)
在交互環(huán)境中的使用示例:
>>> add(2, 3)
5
>>> add(4, 5)
9
>>> add.ncalls
2
>>> 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.ncalls
3
將裝飾器定義成類通常是很簡單的。但是這里還是有一些細(xì)節(jié)需要解釋下,特別是當(dāng)你想將它作用在實(shí)例方法上的時候。
首先,使用 functools.wraps()
函數(shù)的作用跟之前還是一樣,將被包裝函數(shù)的元信息復(fù)制到可調(diào)用實(shí)例中去。
其次,通常很容易會忽視上面的 __get__()
方法。如果你忽略它,保持其他代碼不變再次運(yùn)行,你會發(fā)現(xiàn)當(dāng)你去調(diào)用被裝飾實(shí)例方法時出現(xiàn)很奇怪的問題。例如:
>>> s = Spam()
>>> s.bar(3)
Traceback (most recent call last):
...
TypeError: bar() missing 1 required positional argument: 'x'
出錯原因是當(dāng)方法函數(shù)在一個類中被查找時,它們的 __get__()
方法依據(jù)描述器協(xié)議被調(diào)用,在8.9小節(jié)已經(jīng)講述過描述器協(xié)議了。在這里,__get__()
的目的是創(chuàng)建一個綁定方法對象(最終會給這個方法傳遞self參數(shù))。下面是一個例子來演示底層原理:
>>> s = Spam()
>>> def grok(self, x):
... pass
...
>>> grok.__get__(s, Spam)
<bound method Spam.grok of <__main__.Spam object at 0x100671e90>>
>>>
__get__()
方法是為了確保綁定方法對象能被正確的創(chuàng)建。type.MethodType()
手動創(chuàng)建一個綁定方法來使用。只有當(dāng)實(shí)例被使用的時候綁定方法才會被創(chuàng)建。如果這個方法是在類上面來訪問,那么 __get__()
中的instance參數(shù)會被設(shè)置成None并直接返回 Profiled
實(shí)例本身。這樣的話我們就可以提取它的 ncalls
屬性了。
如果你想避免一些混亂,也可以考慮另外一個使用閉包和 nonlocal
變量實(shí)現(xiàn)的裝飾器,這個在9.5小節(jié)有講到。例如:
import types
from functools import wraps
def 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
@profiled
def add(x, y):
return x + y
這個方式跟之前的效果幾乎一樣,除了對于 ncalls
的訪問現(xiàn)在是通過一個被綁定為屬性的函數(shù)來實(shí)現(xiàn),例如:
>>> add(2, 3)
5
>>> add(4, 5)
9
>>> add.ncalls()
2
>>>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: