Python中的自定义docstring

2024-10-01 13:24:18 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在python中创建自定义docstring?你是说__nameofdocstring__还是你应该做些什么?在

是否可以为某个.py文件创建新的docstring?我想写__notes__ = "blah blah blah",但只是说这句话行不通。在


Tags: pydocstringnotesblahnameofdocstring
1条回答
网友
1楼 · 发布于 2024-10-01 13:24:18

Docstring示例

让我们展示一个多行docstring示例:

def my_function():
"""Do nothing, but document it.

No, really, it doesn't do anything.
"""
pass

让我们看看打印时的效果

^{pr2}$

docstrings声明

下面的Python文件显示了Python中docstring的声明 源文件:

"""
Assuming this is file mymodule.py, then this string, being the
first statement in the file, will become the "mymodule" module's
docstring when the file is imported.
"""

class MyClass(object):
    """The class's docstring"""

    def my_method(self):
        """The method's docstring"""

def my_function():
    """The function's docstring"""

如何访问Docstring

下面是一个交互式会话,演示如何访问docstring

>>> import mymodule
>>> help(mymodule)

假设这是文件我的模块.py然后这个字符串,作为 导入文件时,该文件将成为mymodules modules docstring。在

>>> help(mymodule.MyClass)
The class's docstring

>>> help(mymodule.MyClass.my_method)
The method's docstring

>>> help(mymodule.my_function)
The function's docstring

相关问题 更多 >