def log(func):
def wraper(*a, **kw):
return func(*a, **kw)
return wraper
@log
def f():
print 'f'
print locals()['f'] # - prints <function wraper at 0x00CBF3F0>.
How do you get the real f object (not decorator wrap)?
def log(func):
def wraper(*a, **kw):
return func(*a, **kw)
return wraper
@log
def f():
print 'f'
print locals()['f'] # - prints <function wraper at 0x00CBF3F0>.
How do you get the real f object (not decorator wrap)?
You don't.1 Store it if you need to access it later.
def log(func):
def wrapper(*a, **kw):
return func(*a, **kw)
wrapper.func = func
return wrapper
@log
def f():
print 'f'
print f.func
1 You could mess with the closure, but I can't recommend it.
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.