【问题标题】:What is the python docstring convention for returning multiple values?返回多个值的 python 文档字符串约定是什么?
【发布时间】:2023-03-19 14:48:02
【问题描述】:

假设我有一个返回多个值的函数

import numpy as np
def add_and_raisepower(x, y):
    """
    1) Add inputs **x** and **y** and return the result.

    2) Raise **x** to the power of **y**, multiply this
    result by an array of one's and return the result.

    :type x: float
    :param x: The first input number

    :type y: float
    :param y: The second input number

    :rtype val1: float
    :rtype val2: numpy.array(float)
    """

    val1 = x + y
    val2 = np.ones(100)*(x**y)

    return val1, val2

我关心的是文档字符串中的:rtype: 注释;如果一个函数返回多个值,就像这个例子一样,:rtype: 应该如何写在文档字符串中(根据 PEP-8)?

通常对于只返回一个值的函数,:rtype: 会写成类似

"""
...

:rtype: int
"""

这里没有指定返回的变量名,因为没有关系,因为只有一个返回值。

我知道理想情况下我应该尝试将我的函数分解为更简单的函数,这对于上面的add_and_raisepower 来说当然是可能的,但是我只是用这个作为一个玩具示例来说明这个问题。

【问题讨论】:

标签: python pep8 docstring


【解决方案1】:

在这种情况下,每次的结果都是一个元组。写成这样:

:rtype: (float, numpy.array(float))

【讨论】:

    猜你喜欢
    • 2020-01-28
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2017-10-06
    相关资源
    最近更新 更多