#!/usr/bin/python
from functools import wraps
def logged(func):
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
return with_logging
@logged
def f(x):
"""does some math"""
return x + x * x
I want to know if wraps has the undecorated reference to the function f? I don’t see it when I tried dir(f)
Modified version
#!/usr/bin/python
from functools import wraps
def logged(func):
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
with_logging.undecorated = func
return with_logging
@logged
def f(x):
"""does some math"""
return x + x * x
f.undecorated
No attribute? I was merely following what I used to do with decorator…
There is a reference to the original
fin there, but it’s messy to get to.The first is the wrapped
f, the second is the originalf, notice the hex addresses are different.If you need access to the original
f, I suggest you wrap it differently:Now you have
fandoriginal_f, both usable.