在python中使用装饰器预先添加函数docstring

时间:2012-11-07 02:18:21

标签: python decorator docstring

您如何能够在装饰器前添加函数docstring?

def g(func):
    someOtherDocString = "That is great"
    def wrap(*args, **kwargs):
        func(*args, **kwargs)
return wrap

@g
def f():
""" This is awesome """

结果:

>>>help(f)

Help on function f in module __main__:

f()
    That is great
    That is awesome

非常感谢所有帮助。

1 个答案:

答案 0 :(得分:5)

你尝试过魔法__doc__

from functools import wraps

def g(func):
    func.__doc__ = "That is great" + func.__doc__

    @wraps(func)
    def wrap(*args, **kwargs):
        return func(*args, **kwargs)

    return wrap