問題
你寫了一個裝飾器作用在某個函數(shù)上,但是這個函數(shù)的重要的元信息比如名字、文檔字符串、注解和參數(shù)簽名都丟失了。
解決方案
任何時候你定義裝飾器的時候,都應該使用 functools 庫中的 @wraps 裝飾器來注解底層包裝函數(shù)。例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import time from functools import wraps def timethis(func): ''' Decorator that reports the execution time. ''' @wraps (func) def wrapper( * args, * * kwargs): start = time.time() result = func( * args, * * kwargs) end = time.time() print (func.__name__, end - start) return result return wrapper |
下面我們使用這個被包裝后的函數(shù)并檢查它的元信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
>>> @timethis ... def countdown(n): ... ''' ... Counts down ... ''' ... while n > 0 : ... n - = 1 ... >>> countdown( 100000 ) countdown 0.008917808532714844 >>> countdown.__name__ 'countdown' >>> countdown.__doc__ '\n\tCounts down\n\t' >>> countdown.__annotations__ { 'n' : < class 'int' >} >>> |
討論
在編寫裝飾器的時候復制元信息是一個非常重要的部分。如果你忘記了使用 @wraps
, 那么你會發(fā)現(xiàn)被裝飾函數(shù)丟失了所有有用的信息。比如如果忽略 @wraps
后的效果是下面這樣的:
1
2
3
4
5
6
|
>>> countdown.__name__ 'wrapper' >>> countdown.__doc__ >>> countdown.__annotations__ {} >>> |
@wraps 有一個重要特征是它能讓你通過屬性 __wrapped__ 直接訪問被包裝函數(shù)。例如:
1
2
|
>>> countdown.__wrapped__( 100000 ) >>> |
__wrapped__ 屬性還能讓被裝飾函數(shù)正確暴露底層的參數(shù)簽名信息。例如:
1
2
3
4
|
>>> from inspect import signature >>> print (signature(countdown)) (n: int ) >>> |
一個很普遍的問題是怎樣讓裝飾器去直接復制原始函數(shù)的參數(shù)簽名信息, 如果想自己手動實現(xiàn)的話需要做大量的工作,最好就簡單的使用 @wraps
裝飾器。 通過底層的 __wrapped__
屬性訪問到函數(shù)簽名信息。
以上就是Python如何創(chuàng)建裝飾器時保留函數(shù)元信息的詳細內(nèi)容,更多關(guān)于Python保留函數(shù)元信息的資料請關(guān)注服務器之家其它相關(guān)文章!
原文鏈接:https://python3-cookbook.readthedocs.io/zh_CN/latest/c09/p02_preserve_function_metadata_when_write_decorators.html