首先,宝宝觉着网上没有比我这还透彻的@functools.wraps分析了~~~害羞自恋ing……
栗子详情:http://stackoverflow.com/questions/308999/what-does-functools-wraps-do
step1:
def logged(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
it's exactly the same as saying
def f(x):
"""does some math"""
return x + x * x
f = logged(f)
这时(划重点!),your function f is replaced with the function with_logging.
print f.__name__ #with_logging
print f.__doc__ #什么也没有,因为with_logging没有doc,f才有doc
因为f方法的name doc args也变了
简言之:使用装饰器时,原函数会损失一些信息,