简体   繁体   English

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

[英]Prepending a function docstring with a decorator in python

How would you be able to prepend a functions docstring with a decorator? 您如何能够在装饰器之前添加函数文档字符串?

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

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

result: 结果:

>>>help(f)

Help on function f in module __main__:

f()
    That is great
    That is awesome

All the help would be greatly appreciated. 所有帮助将不胜感激。

Have you tried magic __doc__ : 您是否尝试过魔术__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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 装饰器显示来自装饰器和装饰器的签名和文档字符串 function - Python decorator show signature and docstring from both decorator and decorated function 在Python中使用类属性通过修饰符修改文档字符串 - Using class attributes to modify a docstring with a decorator in Python 用装饰器修改python docstring:这是个好主意吗? - Modifying a python docstring with a decorator: Is it a good idea? Python函数/方法docstring变量 - Python function/method docstring variables 维护签名、允许修改文档字符串并允许可选参数的 Python 装饰器 - Python Decorator that maintains signature, allows modification to docstring, and allows optional arguments Python - 使用decorator.py来保存方法docstring - Python - using decorator.py to preserve a method docstring vim 和 python:[缺少功能文档字符串]缺少 function 或方法文档字符串 - vim with python: [missing-function-docstring] Missing function or method docstring 函数等效于Python中以“ r”开头的字符串 - Function equivalent to prepending string with “r” in Python 没有参数的函数的 Python 谷歌样式 DocString - Python Google-Style DocString for Function with No Arguments 将 function 的文档字符串传递给另一个文件 python3 - Passing docstring of a function to another file python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM