2

你怎么能在函数文档字符串前面加上一个装饰器?

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

所有的帮助将不胜感激。

4

1 回答 1

6

你试过魔法吗__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
于 2012-11-07T02:21:23.390 回答