Function name of wrapped function?
本问题已经有最佳答案,请猛点这里访问。
如何获取原始函数的名称?
1 2 3 4 5 6 7 8 9 10 | def wrap(f): def wrapped_f(*args, **kwargs): # do something return wrapped_f @wrap def A(params): # do something print(A.__name__) |
结果:包好了,但我需要一个!
使用
直接从文件:
Without the use of this decorator factory, the name of the example function would have been 'wrapper', and the docstring of the original example() would have been lost.
号
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from functools import wraps def wrap(f): @wraps(f) def wrapped_f(*args, **kwargs): pass return wrapped_f @wrap def A(params): pass print(A.__name__) |
号
输出:
1 2 3 | $ python -i foo.py A >>> |
手动使用
1 2 3 4 5 6 7 8 9 10 11 12 13 | from functools import wraps def wrap(f): @wraps(f) def wrapped_f(*args, **kwargs): # do something return wrapped_f @wrap def A(params): # do something print(A.__name__) |