Python decorator handling docstrings
Use functools.wraps()
to update the attributes of the decorator:
from functools import wrapsdef decorator(f): @wraps(f) def _decorator(): print 'decorator active' f() return _decorator@decoratordef foo(): '''the magic foo function''' print 'this is function foo'help(foo)
Also see the Standard Library documentation for functools
.
I found a solution, but don't know if it's really nice:
def decorator(f): def _decorator(): print 'decorator active' f() _decorator.__name__=f.__name__ _decorator.__doc__=f.__doc__ return _decorator
The part with _decorator.__name__=f.__name__
seems a little bit hideous... What do you think?