When I wrap a function with @
, how do I make the wrapper function look & feel exactly like the wrapped function? help(function)
in particular.<
Use functools.wraps:
from functools import wraps
def wrapper(f):
@wraps(f)
def call(*args, **kw):
print('in', f, args, kw)
return f(*args, **kw)
return call
@wrapper
def f(a, b = 1, g = g, *args, **kw):
pass
help(f)
Help on function f in module __main__:
f(a, b=1, g=, *args, **kw)
This preserves the __name__
and __doc__
attributes of your wrapped function.