W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
你想給類(lèi)或靜態(tài)方法提供裝飾器。
給類(lèi)或靜態(tài)方法提供裝飾器是很簡(jiǎn)單的,不過(guò)要確保裝飾器在 @classmethod
或 @staticmethod
之前。例如:
import time
from functools import wraps
# A simple decorator
def timethis(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
r = func(*args, **kwargs)
end = time.time()
print(end-start)
return r
return wrapper
# Class illustrating application of the decorator to different kinds of methods
class Spam:
@timethis
def instance_method(self, n):
print(self, n)
while n > 0:
n -= 1
@classmethod
@timethis
def class_method(cls, n):
print(cls, n)
while n > 0:
n -= 1
@staticmethod
@timethis
def static_method(n):
print(n)
while n > 0:
n -= 1
裝飾后的類(lèi)和靜態(tài)方法可正常工作,只不過(guò)增加了額外的計(jì)時(shí)功能:
>>> s = Spam()
>>> s.instance_method(1000000)
<__main__.Spam object at 0x1006a6050> 1000000
0.11817407608032227
>>> Spam.class_method(1000000)
<class '__main__.Spam'> 1000000
0.11334395408630371
>>> Spam.static_method(1000000)
1000000
0.11740279197692871
>>>
如果你把裝飾器的順序?qū)戝e(cuò)了就會(huì)出錯(cuò)。例如,假設(shè)你像下面這樣寫(xiě):
class Spam:
@timethis
@staticmethod
def static_method(n):
print(n)
while n > 0:
n -= 1
那么你調(diào)用這個(gè)鏡頭方法時(shí)就會(huì)報(bào)錯(cuò):
>>> Spam.static_method(1000000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "timethis.py", line 6, in wrapper
start = time.time()
TypeError: 'staticmethod' object is not callable
>>>
問(wèn)題在于 @classmethod
和 @staticmethod
實(shí)際上并不會(huì)創(chuàng)建可直接調(diào)用的對(duì)象, 而是創(chuàng)建特殊的描述器對(duì)象(參考8.9小節(jié))。因此當(dāng)你試著在其他裝飾器中將它們當(dāng)做函數(shù)來(lái)使用時(shí)就會(huì)出錯(cuò)。 確保這種裝飾器出現(xiàn)在裝飾器鏈中的第一個(gè)位置可以修復(fù)這個(gè)問(wèn)題。 當(dāng)我們?cè)诔橄蠡?lèi)中定義類(lèi)方法和靜態(tài)方法(參考8.12小節(jié))時(shí),這里講到的知識(shí)就很有用了。 例如,如果你想定義一個(gè)抽象類(lèi)方法,可以使用類(lèi)似下面的代碼:
from abc import ABCMeta, abstractmethod
class A(metaclass=ABCMeta):
@classmethod
@abstractmethod
def method(cls):
pass
在這段代碼中,@classmethod
跟 @abstractmethod
兩者的順序是有講究的,如果你調(diào)換它們的順序就會(huì)出錯(cuò)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: