在IPython中生成脚本的帮助字符串

时间:2019-08-31 01:47:59

标签: python string ipython documentation

假设mycode.py具有以下通用结构:


import bla
import bla


def function:
    bla bla


description="""My
multi-line description 
of the
code
"""


import bla
more code here

IPython中的哪个命令将打印“ description =“中给出的帮助字符串??

此处所有相关问题似乎都暗示这将是mycode。 doc ,但这不起作用。

2 个答案:

答案 0 :(得分:1)

文档字符串是在模块,函数或类中首先定义且未分配给变量/名称的字符串

my_module.py:

"""
This is the module doc-string
"""

def foo():
    """
    This is the function doc-string
    """
    pass

然后,您可以通过__doc__属性访问文档字符串

import my_module
print(my_module.__doc__)  # This prints "This is the module doc-string"
print(my_module.foo.__doc__)  # This prints "This is the function doc-string"

答案 1 :(得分:1)

@Iain Shelvington's answer中添加有关在何处放置文档字符串的信息,在交互式环境中,人们通常可以使用help函数来访问它(请注意,此函数不仅适用于模块,类和函数以及关键字字符串等其他内容。

help(object)

此外,在IPython中,有一个magic command %pinfo <object>用于检索对象的信息,包括文档字符串。另一个经常使用的较短的别名是<object>?。因此,如果代码位于my_module.py中,则在导入后,可以使用my_module?访问其信息。