【问题标题】:Is it possible to get doctring of coroutine?是否有可能获得协同程序的文档?
【发布时间】:2020-03-08 16:21:13
【问题描述】:

我得到了一个由以下定义的函数:

@bot.command()
async def ping(ctx):
    """
        return pong
    """
    await ctx.send("pong !")

但我会使用它的文档来提供比不和谐模块更好的功能帮助。我怎么会有文档字符串?

我试过了:

__doc__ ( inside ping function )
ping.__doc__

【问题讨论】:

  • 如果 @bot.command() 装饰器表现良好,ping.__doc__ 应该可以工作。
  • 如果我在 func 之外尝试,它会将@bot.command 的 doc 归还给我

标签: python discord.py docstring


【解决方案1】:

您可以从the implementation of command 看到您的func 被包裹在Command 对象中:

def command(name=None, cls=None, **attrs):
    """..."""
    if cls is None:
        cls = Command

    def decorator(func):
        if isinstance(func, Command):
            raise TypeError('Callback is already a command.')
        return cls(func, name=name, **attrs)

    return decorator

该装饰器的文档字符串指出:

默认情况下,help 属性会自动从 函数的文档字符串,并使用 inspect.cleandoc。如果文档字符串为bytes,则对其进行解码 使用 utf-8 编码进入str

您可以在the implementation of Command 中看到这种情况,因此您应该能够通过ping.help 看到您的文档字符串。

【讨论】:

    猜你喜欢
    • 2013-06-14
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 2019-02-12
    • 2020-01-13
    • 2021-10-30
    • 1970-01-01
    相关资源
    最近更新 更多