#!/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
我想知道wraps
函数 f 是否有未修饰的引用?我试过的时候没看到dir(f)
修改版
#!/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
没有属性?我只是在遵循我过去对装饰者所做的事情......