关于 python __doc__ 文档字符串

发布于 2024-09-26 04:04:08 字数 174 浏览 0 评论 0原文

我想显示我的函数的文档字符串, 但如果我这样使用

@cost_time
def func():
    "define ...."
    blabla
print func.__doc__

它不会显示文档字符串,只是因为我使用了一些元编程技巧, 如何解决这个问题?

i want to show docstring of my function,
but if i use like this

@cost_time
def func():
    "define ...."
    blabla
print func.__doc__

it will not show the docstring,just because i use some meta programming tricky,
how can fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风起 2024-10-03 04:04:08

cost_time 装饰器返回的包装函数必须具有文档字符串,而不是 func。因此,正确使用 functools.wraps设置 __name____doc__

from functools import wraps

def cost_time(fn):
    @wraps(fn)
    def wrapper():
        return fn()

    return wrapper

Your wrapped function returned from the cost_time decorator must have the docstring instead of func. Therefore, use functools.wraps which correctly sets __name__ and __doc__:

from functools import wraps

def cost_time(fn):
    @wraps(fn)
    def wrapper():
        return fn()

    return wrapper
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文