关于Python的__doc__文档字符串
我想显示我函数的文档字符串,但如果我这样写
@cost_time
def func():
"define ...."
blabla
print func.__doc__
它就不会显示文档字符串,只是因为我用了些元编程的小技巧,怎么才能解决这个问题呢?
2 个回答
2
12
你从 cost_time
装饰器返回的函数必须有文档字符串,而不是 func
。所以,要使用 functools.wraps
,它可以正确设置 __name__
和 __doc__
:
from functools import wraps
def cost_time(fn):
@wraps(fn)
def wrapper():
return fn()
return wrapper