python 裝飾器的基本使用
def my_decorator(func): def wrapper(): print(’wrapper of decorator’) func() return wrapper()def test(): print(’test done.’)test = my_decorator(test)test輸出:wrapper of decoratortest done.
這段代碼中,變量test指向了內(nèi)部函數(shù)wrapper(), 而內(nèi)部函數(shù)wrapper()中又會(huì)調(diào)用原函數(shù)test(),因此最后調(diào)用test()時(shí),就會(huì)打印’wrapper of decorator’ 然后輸出 ’test done.’
這里的函數(shù)my_decorator()就是一個(gè)裝飾器,它把真正需要執(zhí)行的函數(shù)test()包裹在其中,并且改變了它的行為,但是原函數(shù)test()不變。
上述代碼在Python中更簡單、更優(yōu)雅的表示:
def my_decorator(func): def wrapper(): print(’wrapper of decorator’) func() return wrapper()@my_decoratordef test(): print(’test done.’)test
這里的@, 我們稱為語法糖,@my_decorator就相當(dāng)于前面的test=my_decorator(test)語句
如果程序中又其他函數(shù)需要類似裝飾,只需要加上@decorator就可以,提高函數(shù)的重復(fù)利用和程序可讀性
帶有參數(shù)的裝飾器def args_decorator(func): def wrapper(*args, **kwargs): print(’wrapper of decorator’) func(*args, **kwargs) return wrapper@args_decoratordef identity(name, message): print(’identity done.’) print(name, message)identity(’changhao’, ’hello’)輸出:wrapper of decoratoridentity done.changhao hello
通常情況下,會(huì)把a(bǔ)rgs和*kwargs,作為裝飾器內(nèi)部函數(shù)wrapper()的參數(shù)。 表示接受任意數(shù)量和類型的參數(shù)
帶有自定義參數(shù)的裝飾器定義一個(gè)參數(shù),表示裝飾器內(nèi)部函數(shù)被執(zhí)行的次數(shù),可以寫成這個(gè)形式:
def repeat(num): def my_decorator(func): def wrapper(*args, **kwargs): for i in range(num):func(*args, **kwargs) return wrapper return my_decorator@repeat(3)def showname(message): print(message)showname(’changhao’)輸出:changhaochanghaochanghao類裝飾器
類也可以作裝飾器,類裝飾器主要依賴于函數(shù) __call__每當(dāng)調(diào)用一個(gè)示例時(shí),函數(shù)__call__()就會(huì)被執(zhí)行一次。
class Count: def __init__(self, func): self.func = func self.num_calls = 0 def __call__(self, *args, **kwargs): self.num_calls += 1 print(’num of calls is: {}’.format(self.num_calls)) return self.func(*args, **kwargs)@Countdef example(): print(’example done.’)example()example()輸出:num of calls is: 1example done.num of calls is: 2example done.
這里定義了類Count,初始化時(shí)傳入原函數(shù)func(),而__call__()函數(shù)表示讓變量num_calls自增1,然后打印,并且調(diào)用原函數(shù)。因此我們第一次調(diào)用函數(shù)example()時(shí),num_calls的值是1,而第一次調(diào)用時(shí),值變成了2。
裝飾器的嵌套import functoolsdef my_decorator1(func): @functools.wraps(func) def wrapper(*args, **kwargs): print(’execute decorator1’) func(*args, **kwargs) return wrapperdef my_decorator2(func): @functools.wraps(func) def wrapper(*args, **kwargs): print(’execute decorator2’) func(*args, **kwargs) return wrapper@my_decorator1@my_decorator2def test2(message): print(message)test2(’changhao’)輸出:execute decorator1execute decorator2changhao類裝飾器
類也可以作裝飾器,類裝飾器主要依賴于函數(shù) __call__每當(dāng)調(diào)用一個(gè)示例時(shí),函數(shù)__call__()就會(huì)被執(zhí)行一次。
class Count: def __init__(self, func): self.func = func self.num_calls = 0 def __call__(self, *args, **kwargs): self.num_calls += 1 print(’num of calls is: {}’.format(self.num_calls)) return self.func(*args, **kwargs)@Countdef example(): print(’example done.’)example()example()輸出:num of calls is: 1example done.num of calls is: 2example done.
這里定義了類Count,初始化時(shí)傳入原函數(shù)func(),而__call__()函數(shù)表示讓變量num_calls自增1,然后打印,并且調(diào)用原函數(shù)。因此我們第一次調(diào)用函數(shù)example()時(shí),num_calls的值是1,而第一次調(diào)用時(shí),值變成了2。
裝飾器的嵌套import functoolsdef my_decorator1(func): @functools.wraps(func) def wrapper(*args, **kwargs): print(’execute decorator1’) func(*args, **kwargs) return wrapperdef my_decorator2(func): @functools.wraps(func) def wrapper(*args, **kwargs): print(’execute decorator2’) func(*args, **kwargs) return wrapper@my_decorator1@my_decorator2def test2(message): print(message)test2(’changhao’)輸出:execute decorator1execute decorator2changhao@functools.wrap裝飾器使用
import functoolsdef my_decorator(func): @functools.wraps(func) def wrapper(*args, **kwargs): print(’wrapper of decorator’) func(*args, **kwargs) return wrapper@my_decoratordef test3(message): print(message)test3.__name__ 輸出test3
通常使用內(nèi)置的裝飾器@functools.wrap,他會(huì)保留原函數(shù)的元信息(也就是將原函數(shù)的元信息,拷貝到對(duì)應(yīng)的裝飾器里)
裝飾器用法實(shí)例身份認(rèn)證import functoolsdef authenticate(func): @functools.wraps(func) def wrapper(*args, **kwargs): request = args[0] if check_user_logged_in(request): return func(*args, **kwargs) else: raise Exception(’Authentication failed’) return wrapper@authenticatedef post_comment(request): pass
這段代碼中,定義了裝飾器authenticate;而函數(shù)post_comment(),則表示發(fā)表用戶對(duì)某篇文章的評(píng)論。每次調(diào)用這個(gè)函數(shù)前,都會(huì)檢查用戶是否處于登錄狀態(tài),如果是登錄狀態(tài),則允許這項(xiàng)操作;如果沒有登錄,則不允許。
日志記錄import timeimport functoolsdef log_execution_time(func): @functools.wraps(func) def wrapper(*args, **kwargs): start = time.perf_counter() res = func(*args, **kwargs) end = time.perf_counter() print(’{} took {} ms’.format(func.__name__, (end - start) * 1000)) return wrapper@log_execution_timedef calculate_similarity(times): pass
這里裝飾器log_execution_time記錄某個(gè)函數(shù)的運(yùn)行時(shí)間,并返回其執(zhí)行結(jié)果。如果你想計(jì)算任何函數(shù)的執(zhí)行時(shí)間,在這個(gè)函數(shù)上方加上@log_execution_time即可。
總結(jié)所謂裝飾器,其實(shí)就是通過裝飾器函數(shù),來修改原函數(shù)的一些功能,使得原函數(shù)不需要修改。
以上就是python 裝飾器的基本使用的詳細(xì)內(nèi)容,更多關(guān)于python 裝飾器的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁2. ajax請(qǐng)求添加自定義header參數(shù)代碼3. Android實(shí)現(xiàn)觸發(fā)html頁面的Button控件點(diǎn)擊事件方式4. JavaScript設(shè)計(jì)模式之策略模式實(shí)現(xiàn)原理詳解5. Nginx+php配置文件及原理解析6. 解決Python 進(jìn)程池Pool中一些坑7. PHP8.0新功能之Match表達(dá)式的使用8. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究9. 八種Vue組件間通訊方式合集(推薦)10. JavaScript基于用戶照片姓名生成海報(bào)
