python functools
2021-06-27 20:06
# functools模块
# update_wrapper(wapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
# 类似copy_properties功能
# wrapper包装函数,wrapped被包装函数
# 元组WRAPPER_ASSIGNMENTS中是要被覆盖的属性
# __model__(模块名),__name__(名字),__qualname__(限定名),__doc__(文档),__annotations(参数注解)__
# 元组WRAPPER_UPDATES中是要被更新的属性,__dict__属性字典
# 增加一个__wrapped__属性,保存wrapped函数import functools
def logger(fn):
def wrapper(*args, **kwargs):
print(‘before‘)
ret = fn(*args, **kwargs)
print(‘after‘)
return ret
print(‘{} {}‘.format(id(wrapper), id(fn)))
return functools.update_wrapper(wrapper, fn) # update_wrapper返回值就是wrapper@logger
def add(x, y):
‘‘‘
This is a fun
:param x: int
:param y: int
:return: int
‘‘‘
ret = x + y
return retprint(‘1 -----------------------------------------‘)
print(add.__name__, add.__doc__, add.__qualname__, sep=‘\n‘)
print(id(add.__wrapped__)) # 被包装函数的内存地址# functools模块
# wraps(wrapped, assigned = WRAPPER_ASSIGNMENTS, updated = WRAPPER_UPDATES) **消除装饰器副作用
# 类似copy_properties功能
# wrapped被包装函数
# 元组WRAPPER_ASSIGNMENTS中是要被覆盖的属性
# __model__(模块名),__name__(名字),__qualname__(限定名),__doc__(文档),__annotations(参数注解)__
# 元组WRAPPER_UPDATES中是要被更新的属性,__dict__属性字典
# 增加一个__wrapped__属性,保存wrapped函数import functools
def logger(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
print(‘before‘)
ret = fn(*args, **kwargs)
print(‘after‘)
return ret
print(‘{} {}‘.format(id(wrapper), id(fn)))
return wrapper@logger
def add(x, y):
‘‘‘
This is a fun
:param x: int
:param y: int
:return: int
‘‘‘
ret = x + y
return retprint(‘2 -----------------------------------------‘)
print(add.__name__, add.__doc__, add.__qualname__, sep=‘\n‘)
print(id(add.__wrapped__)) # 被包装函数的内存地址
print(add(4, 40))