【问题标题】:How to document multiple return values using reStructuredText in Python 2?如何在 Python 2 中使用 reStructuredText 记录多个返回值?
【发布时间】:2017-02-07 03:24:33
【问题描述】:

Python docs 表示“用于 Python 文档的标记是 reStructuredText”。我的问题是:应该如何编写块注释以显示多个返回值?

def func_returning_one_value():
    """Return just one value.

    :returns: some value
    :rtype: str
    """

def func_returning_three_values():
    """Return three values.

    How do I note in reStructuredText that three values are returned?
    """

我在 Python 文档中使用 reStructuredText 找到了tutorial,但它没有记录多个返回值的示例。 Sphinx docs on domains 讨论了 returnsrtype,但没有讨论多个返回值。

【问题讨论】:

  • 似乎任何东西都与文档字符串有关,只要它在整个项目中清晰、简洁和一致。写一些适合你目的的东西。 PEP 257 有一些广泛的约定。查看计算机上的 Python dot py 文件,看看开发人员是如何做到的。
  • os.walk() 一样返回多个东西。

标签: python python-2.7 documentation restructuredtext


【解决方案1】:

有一个折中的解决方案:只写普通的 Markdown 文本。 例如

def func(a, b):
    """

    :param int a: first input
    :param int a: second input
    :returns: 
        - x - first output
        - y - second output
    """

    return x, y

这将生成以下文档:

几乎是我们想要的,对吧?

这样做的缺点是您不能为每个元素指定返回类型。你得自己写,比如

"""
:returns:
    -x (:py:class:`int`) - first output
"""

【讨论】:

    【解决方案2】:

    正如 wwi 在 cmets 中提到的,要使用的详细格式没有严格定义。

    就我自己而言,我通常使用您在上面使用的Field List 表示法样式。它支持换行,所以只要在你认为有必要的地方换行就行了

    def my_func(param1, param2):
        """
        This is a sample function docstring
    
        :param param1: this is a first param
        :param param2: this is a second param
        :returns: tuple (result1, result2) 
            WHERE
            str result1 is .... 
            str result2 is ....        
        """
    

    【讨论】:

    • 不适用于我。您是否必须安装一些特殊的东西,或者告诉 Sphinx 它必须使用字段列表?
    猜你喜欢
    • 2020-01-28
    • 1970-01-01
    • 2014-05-15
    • 2019-09-27
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多