Skip to content Skip to sidebar Skip to footer

Get Decorated Function Object By String Name

def log(func): def wraper(*a, **kw): return func(*a, **kw) return wraper @log def f(): print 'f' print locals()['f'] # - prints

Solution 1:

The functools module also provides a wraps decorator which makes sure that the wrapped function looks more like the real function: correct name, module, and docstring, for example.

Solution 2:

If you're running python 3.2 or above, and you use functools.wraps then you will find the wrapped function on the __wrapped__ attribute:

from functools import wraps

deflog(func):
    @wraps(func)defwrapper(*a, **kw):
        return func(*a, **kw)
    return wrapper

@logdeff():
    print'f'print f.__wrapped__

functools.wrapsis a convenience function for decorating a decorated function with the function that does all the work, including adding this attribute functools.update_wrapper.

Post a Comment for "Get Decorated Function Object By String Name"