Python 装饰器处理文档字符串

发布于 2024-08-12 03:32:30 字数 643 浏览 13 评论 0 原文

我在使用带有装饰器的文档字符串时遇到问题。给出以下示例:

def decorator(f):
    def _decorator():
        print 'decorator active'
        f()
    return _decorator

@decorator
def foo():
    '''the magic foo function'''
    print 'this is function foo'

help(foo)

现在帮助没有按预期显示 foo 的文档字符串,它显示:

Help on function _decorator in module __main__:

_decorator()

没有装饰器,帮助是正确的:

Help on function foo in module __main__:

foo()
    the magic foo function

我知道,函数 foo< /code> 被装饰器包装,因此函数对象不再是函数 foo 。但是,有什么好的解决方案可以按预期获取文档字符串(和帮助)呢?

I have a problem using docstrings with decorators. Given the following example:

def decorator(f):
    def _decorator():
        print 'decorator active'
        f()
    return _decorator

@decorator
def foo():
    '''the magic foo function'''
    print 'this is function foo'

help(foo)

Now the help doesn't show me the docstring of foo as expected, it shows:

Help on function _decorator in module __main__:

_decorator()

Without the decorator, the help is correct:

Help on function foo in module __main__:

foo()
    the magic foo function

I know, that the function foo is wrapped by the decorator, and so the function object is not the function foo any more. But what is a nice solution to get the docstring (and the help) as expected?

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

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

发布评论

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

评论(4

陌路黄昏 2024-08-19 03:32:30

使用 functools.wraps() 更新装饰器的属性:

from functools import wraps

def decorator(f):
    @wraps(f)
    def _decorator():
        print 'decorator active'
        f()
    return _decorator

@decorator
def foo():
    '''the magic foo function'''
    print 'this is function foo'

help(foo)

另请参阅 functools 的标准库文档

Use functools.wraps() to update the attributes of the decorator:

from functools import wraps

def decorator(f):
    @wraps(f)
    def _decorator():
        print 'decorator active'
        f()
    return _decorator

@decorator
def foo():
    '''the magic foo function'''
    print 'this is function foo'

help(foo)

Also see the Standard Library documentation for functools.

歌枕肩 2024-08-19 03:32:30

我找到了一个解决方案,但不知道它是否真的很好:

def decorator(f):
    def _decorator():
        print 'decorator active'
        f()
    _decorator.__name__=f.__name__
    _decorator.__doc__=f.__doc__
    return _decorator

_decorator.__name__=f.__name__ 的部分看起来有点丑陋......你觉得怎么样?

I found a solution, but don't know if it's really nice:

def decorator(f):
    def _decorator():
        print 'decorator active'
        f()
    _decorator.__name__=f.__name__
    _decorator.__doc__=f.__doc__
    return _decorator

The part with _decorator.__name__=f.__name__ seems a little bit hideous... What do you think?

孤独患者 2024-08-19 03:32:30

解决方案非常简单。文档字符串应该在主函数之上调用的最顶层装饰器中提及。查找下面的示例:

import math

def wrap2(func):
    def squarer(x):
        return [math.sqrt(i) for i in func(x)]
    return squarer
    

def wrap1(func):
    def summer(x):
        return [i*2 for i in func(x)]
    return summer

def wrap3(func):
    def rounder(x):
        return [round(i,1) for i in func(x)]
    return rounder

def wrap4(func):
    def stringer(x):
        '''
    Enter the input of a 2-dim array to get the output
    '''
        return [str(i)+' rounds ' for i in func(x)]
    return stringer

@wrap4
@wrap3
@wrap2
@wrap1
def rooter(i):
    return [sum(p) for p in i]

help(rooter)

# output
Help on function stringer in module __main__:

stringer(x)
    Enter the input of a 2-dim array to get the output

---or----

Signature: rooter(x)
Docstring: Enter the input of a 2-dim array to get the output
File:      d:\<ipython-input-392-487fc73b05cf>
Type:      function

因此,必须在 wrap4 装饰器函数 中提及 Doc String,才能使其在 main 函数中可见。

The solution is pretty easy. The doc string should be mentioned in the top most decorator that is being called on top of the main function. Find the example below:

import math

def wrap2(func):
    def squarer(x):
        return [math.sqrt(i) for i in func(x)]
    return squarer
    

def wrap1(func):
    def summer(x):
        return [i*2 for i in func(x)]
    return summer

def wrap3(func):
    def rounder(x):
        return [round(i,1) for i in func(x)]
    return rounder

def wrap4(func):
    def stringer(x):
        '''
    Enter the input of a 2-dim array to get the output
    '''
        return [str(i)+' rounds ' for i in func(x)]
    return stringer

@wrap4
@wrap3
@wrap2
@wrap1
def rooter(i):
    return [sum(p) for p in i]

help(rooter)

# output
Help on function stringer in module __main__:

stringer(x)
    Enter the input of a 2-dim array to get the output

---or----

Signature: rooter(x)
Docstring: Enter the input of a 2-dim array to get the output
File:      d:\<ipython-input-392-487fc73b05cf>
Type:      function

So, the Doc String must be mentioned in the wrap4 decorator function for it to be visible in the main function.

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