__doc__
在功能中提供一些文档似乎很有用
这是真的。除功能外,文档也可以在模块中提供。因此,如果您有一个mymodule.py
像这样的文件:
"""This is the module docstring."""
def f(x):
"""This is the function docstring."""
return 2 * x
您可以像这样访问其文档字符串:
>>> import mymodule
>>> mymodule.__doc__
'This is the module docstring.'
>>> mymodule.f.__doc__
'This is the function docstring.'
现在,回到您的问题:做print(__doc__)
什么?简而言之:它输出模块文档字符串。如果未指定文档字符串,则__doc__
默认为None
。