【问题标题】:How to get just summary line from python docstring?如何从 python 文档字符串中获取摘要行?
【发布时间】:2019-03-22 10:48:01
【问题描述】:

我编写测试函数。在文档字符串中,我通常会提到测试用例名称作为摘要行。测试用例描述如下。现在我只需要从 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

所以我需要一种方法来打印文档字符串的第一行,即“这是 test_filesystem_001 的测试用例名称。” 提前致谢。

【问题讨论】:

  • 这显然不是上述链接的重复。这个问题特定于从文档字符串中获取摘要/第一行。
  • 是的,但是从副本中不明显的部分在您的问题中没有明确定义。
  • 无论如何,我的问题得到了适当的答案。

标签: python docstring


【解决方案1】:

刚拿到第一行:

>>>test_filesystem_001.__doc__.split("\n")[0]
This is test case name of test_filesystem_001.

你 split __doc__ 换行。这将返回新行之前的部分和新行之后的部分的数组。要访问第一部分,请使用[0]

【讨论】:

    【解决方案2】:

    获取文档字符串,将其分成几行,然后取第一行。

    print test_filesystem_001.__doc__.splitlines()[0]
    

    【讨论】:

      猜你喜欢
      • 2011-07-25
      • 2022-01-25
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多