Fstring没有作为文档字符串读取[不一致.py]



我的代码中有一个问题,我似乎找不到解决的方法。我有一个自定义帮助命令,工作得很好但当我将docstring定义为fstring时它会将None作为输出

代码从我的帮助命令:

if cmd.help:
emd.add_field(name=f'{self.client.command_prefix}{cmd}', value=f'{aliases}n{cmd.help}u200b', inline=False)
found_cmd = True
else:
print(cmd.help)
emd.add_field(name=f'{self.client.command_prefix}{cmd}', value=f'{aliases}n - u200b', inline=False)

roll命令我试过test命令:

@commands.command()
async def roll(self, ctx, sides:int = 6, num_of_times_to_roll:int=1):
f'''syntax:`{self.client.command_prefix}roll [number of sides] [number of times to roll]` nThis command rolls a die or dice with any number of sides and any number of times nn`{self.client.command_prefix}roll_side` nThis command only takes the number of sides as an input'''

我认为python不读取字符串作为文档字符串,因为当我尝试使用正常文档字符串的帮助命令时,它工作得很好。如果有人知道如何解决这个问题,请告诉我。提前谢谢。

这是因为python的f-string是一个运行时工具,在运行时膨胀字符串. 另一方面,文档字符串编译时的甚至在解释代码之前就被解析的工具。简单地说,您无法从编译时的运行时设施中获益,因为实际上没有任何东西在运行!
想了解更多信息,这篇文章可能会有所帮助。

格式化字符串不能用作文档字符串,即使它们不这样做包括表达式。

的例子:

>>> def foo():
...     f"Not a docstring"
...
>>> foo.__doc__ is None
True

在这个答案中有一个可能的解决方案

最新更新