在函数对象中保存着一些函数的元数据
import pprint
from functools import update_wrapper, wraps, WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES
def func(a):
return a +2
使用标准库functools中的装饰器wraps装饰器内内部包裹函数,可以指定原函数的某些属性,更新到包裹函数上面
def mydecorator(func):
def wrapper(*args, **kwargs):
'''wrapper function'''
print('in wrapper')
func(*args, **kwargs)
update_wrapper(wrapper, func, ('__name__', '__doc__'), ('__dict__',))
return wrapper
def mydecorator(func):
def wrapper(*args, **kwargs):
'''wrapper function'''
print('in wrapper')
func(*args, **kwargs)
update_wrapper(wrapper, func)
return wrapper
def mydecorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
'''wrapper function'''
print('in wrapper')
func(*args, **kwargs)
return wrapper
@mydecorator
def example():
'''example function'''
print('in example')
print(example.__name__)
print(example.__doc__)
print(WRAPPER_ASSIGNMENTS)
print(WRAPPER_UPDATES)