【问题标题】:Prepending a function docstring with a decorator in python在 python 中使用装饰器添加函数文档字符串
【发布时间】:2012-11-07 02:18:20
【问题描述】:

如何在函数文档字符串前添加装饰器?

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

我们将不胜感激。

【问题讨论】:

    标签: python decorator docstring


    【解决方案1】:

    你试过魔术吗__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
    

    【讨论】:

      猜你喜欢
      • 2020-06-08
      • 1970-01-01
      • 2010-12-19
      • 2021-03-19
      • 1970-01-01
      • 2023-03-18
      • 2021-01-05
      • 2015-09-05
      • 2023-03-17
      相关资源
      最近更新 更多