是否有可能拥有“动态”帮助文本?

问题描述 投票:1回答:1

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

def my_func(): 
    "help text"

这样help(my_func)将打印help text

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

例如:

test = "123"

def my_func():
     "help text" + test

不会为qazxsw poi打印任何东西

python function
1个回答
9
投票

您可以覆盖help(my_func)属性

__doc__

来自test = "123" def my_func(): pass my_func.__doc__ = "help text" + test

docstring是一个字符串文字,作为模块,函数,类或方法定义中的第一个语句出现。这样的文档字符串成为该对象的PEP-0257特殊属性。

© www.soinside.com 2019 - 2024. All rights reserved.