从函数中获取docstring

时间:2023-01-11 01:43:23

I have the following function:

我有以下功能:

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

How do I get access to the docstring?

如何访问docstring?

3 个解决方案

#1


167  

Interactively, you can display it with

交互地,您可以显示它与

help(my_func)

Or from code you can retrieve it with

或者从代码中检索它

my_func.__doc__

#2


58  

You can also use inspect.getdoc. It cleans up the __doc__ by normalizing tabs to spaces and left shifting the doc body to remove common leading spaces.

您还可以使用inspection .getdoc。它通过将标签规范化到空格来清除__doc__,并向左移动文档体以移除公共的引导空间。

#3


1  

On ipython or jupyter notebook, you can use all the above mentioned ways, but i go with

在ipython或jupyter笔记本上,您可以使用上面提到的所有方法,但是我支持

my_func?

or

?my_func

for quick summary of both method signature and docstring.

快速总结方法签名和docstring。

I avoid using

我避免使用

my_func??

(as commented by @rohan) for docstring and use it only to check the source code

(如@rohan所言)用于docstring,并仅用于检查源代码

#1


167  

Interactively, you can display it with

交互地,您可以显示它与

help(my_func)

Or from code you can retrieve it with

或者从代码中检索它

my_func.__doc__

#2


58  

You can also use inspect.getdoc. It cleans up the __doc__ by normalizing tabs to spaces and left shifting the doc body to remove common leading spaces.

您还可以使用inspection .getdoc。它通过将标签规范化到空格来清除__doc__,并向左移动文档体以移除公共的引导空间。

#3


1  

On ipython or jupyter notebook, you can use all the above mentioned ways, but i go with

在ipython或jupyter笔记本上,您可以使用上面提到的所有方法,但是我支持

my_func?

or

?my_func

for quick summary of both method signature and docstring.

快速总结方法签名和docstring。

I avoid using

我避免使用

my_func??

(as commented by @rohan) for docstring and use it only to check the source code

(如@rohan所言)用于docstring,并仅用于检查源代码