有没有可能有一个“动态”的帮助文本?

2024-05-08 03:04:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以提供如下帮助文本:

def my_func(): 
    "help text"

这样help(my_func)将打印help text。你知道吗

是否可以从全局变量构造帮助字符串?你知道吗

例如:

test = "123"

def my_func():
     "help text" + test

不会为help(my_func)打印任何内容


Tags: 字符串texttest文本内容mydefhelp
1条回答
网友
1楼 · 发布于 2024-05-08 03:04:22

您可以重写__doc__属性

test = "123"

def my_func():
    pass

my_func.__doc__ = "help text" + test

PEP-0257

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object.

相关问题 更多 >