如何从 python 文档字符串中获取摘要行?
How to get just summary line from python docstring?
我写测试函数。在文档字符串中,我通常会提到测试用例名称作为摘要行。测试用例描述如下。现在我只需要从文档字符串中获取测试用例名称(第一行)。有什么pythonic方法可以做到吗?
def test_filesystem_001():
"""This is test case name of test_filesystem_001.
[Test Description]
-Create a file
-write some data
-delete it
"""
pass
所以我需要一种方法来打印文档字符串的第一行,即 "This is test case name of test_filesystem_001."
提前致谢。
刚刚得到第一行:
>>>test_filesystem_001.__doc__.split("\n")[0]
This is test case name of test_filesystem_001.
您 split
将 __doc__
字符串换行。这个returns换行前的部分和换行后的部分组成的数组。要访问第一部分,请使用 [0]
获取文档字符串,将其拆分成行,然后获取第一个。
print test_filesystem_001.__doc__.splitlines()[0]
我写测试函数。在文档字符串中,我通常会提到测试用例名称作为摘要行。测试用例描述如下。现在我只需要从文档字符串中获取测试用例名称(第一行)。有什么pythonic方法可以做到吗?
def test_filesystem_001():
"""This is test case name of test_filesystem_001.
[Test Description]
-Create a file
-write some data
-delete it
"""
pass
所以我需要一种方法来打印文档字符串的第一行,即 "This is test case name of test_filesystem_001." 提前致谢。
刚刚得到第一行:
>>>test_filesystem_001.__doc__.split("\n")[0]
This is test case name of test_filesystem_001.
您 split
将 __doc__
字符串换行。这个returns换行前的部分和换行后的部分组成的数组。要访问第一部分,请使用 [0]
获取文档字符串,将其拆分成行,然后获取第一个。
print test_filesystem_001.__doc__.splitlines()[0]