Skip to main content
 首页 » 编程设计

python之是否有可能有 "dynamic"帮助文本

2025年05月04日10jackei

我可以提供这样的帮助文本:

def my_func():  
    "help text" 

help(my_func) 将打印帮助文本。

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

例如这个:

test = "123" 
 
def my_func(): 
     "help text" + test 

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

请您参考如下方法:

你可以覆盖__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.