相关疑难解决方法(0)

Python装饰器处理docstrings

使用带有装饰器的docstrings时遇到问题.给出以下示例:

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)
Run Code Online (Sandbox Code Playgroud)

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

Help on function _decorator in module __main__:

_decorator()
Run Code Online (Sandbox Code Playgroud)

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

Help on function foo in module __main__:

foo()
    the magic foo function
Run Code Online (Sandbox Code Playgroud)

我知道,该函数foo由装饰器包装,因此函数对象不再是该函数foo.但是,按预期获得文档字符串(和帮助)的好方法是什么?

python docstring decorator

51
推荐指数
2
解决办法
1万
查看次数

标签 统计

decorator ×1

docstring ×1

python ×1