Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从函数中获取docstring_Python - Fatal编程技术网

Python 从函数中获取docstring

Python 从函数中获取docstring,python,Python,我有以下功能: def my_func(): """My docstring is both funny and informative""" pass 如何访问文档字符串?以交互方式,您可以使用 help(my_func) 或者从您可以使用的代码中检索它 my_func.__doc__ 您也可以使用。它通过将制表符规范化为空格,并向左移动文档正文以删除常用的前导空格,来清理\uuuu文档 在ipython或jupyter笔记本上,您可以使用上述所有方法,但我同意 my_f

我有以下功能:

def my_func():
    """My docstring is both funny and informative"""
    pass

如何访问文档字符串?

以交互方式,您可以使用

help(my_func)
或者从您可以使用的代码中检索它

my_func.__doc__

您也可以使用。它通过将制表符规范化为空格,并向左移动文档正文以删除常用的前导空格,来清理
\uuuu文档

在ipython或jupyter笔记本上,您可以使用上述所有方法,但我同意

my_func?

用于快速总结方法签名和docstring

我避免使用

my_func??

(正如@rohan所评论的)用于docstring,并仅用于检查源代码

BTW:此技术适用于内置函数以及模块和类(也适用于对象的test help(),可能也适用)。这使您的python shell成为一个交互式帮助shell!在ipython提示符下,您可以执行我的功能??帮助(func)给出一个可读的输出,而func.\uu doc\uuu给出一个内联输出。感谢您的回答。请注意,使用-OO运行Python会去掉docstring。如果您打算将代码分发给其他人,请记住他们可能会以这种方式运行代码。如果你的代码依赖于docstrings,但却没有抓住那些不存在docstrings的情况,这会让那些人非常不高兴。
my_func??