This article mainly introduces in detail how python retains metadata for decorated functions. I hope it can help you.
Case:
The function object stores some function metadata, such as:
f.__name__ Function name
f.__defaults__
After using the decorator, when accessing the above properties in the decorator, what we see is the metadata of the decorator function
Requirements:
Implemented in the decorator function, retaining the metadata of the decorated function
How to implement this?implemented through the wraps and update_wrapper methods in functools, each of which can be implemented independently
#!/usr/bin/python3 import time from functools import (wraps, update_wrapper, WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES) def count_time(func): """ 给目标函数加上计算运行时间统计 """ # 这个装上器和update_wrapper一样,默认参数WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES @wraps(func) def wrapper(*args, **kwargs): start_time = time.time() # 定义result接收函数返回值,并且在装饰函数最后返回回去 resutl = func(*args, **kwargs) print('运行时间:', time.time()-start_time) return resutl # 其中默认参数 WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES # update_wrapper(wrapper, func) return wrapper @count_time def add(num=100): """ 计算 0~num 累加值,默认num=100 """ time.sleep(1) return sum([x for x in range(num+1)]) if __name__ == '__main__': print('函数名:', add.__name__) print('属性字典:', add.__dict__) print('函数默认参数:', add.__defaults__) print('函数所在模块:', add.__module__) print('函数文档:', add.__doc__) # 打印两个默认参数 print(WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES) result = add() print(result)
The above is the detailed content of Python retains metadata methods for decorated functions. For more information, please follow other related articles on the PHP Chinese website!