返回多个值的 python 文档字符串约定是什么?

PyR*_*red 1 python docstring pep8

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

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
Run Code Online (Sandbox Code Playgroud)

我关心的是:rtype:文档字符串中的评论;如果函数返回多个值(如本示例所示),则应如何:rtype:在文档字符串中编写(根据 PEP-8)?

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

"""
...

:rtype: int
"""
Run Code Online (Sandbox Code Playgroud)

其中未指定返回的变量名称并不重要,因为只有一个返回值。

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

小智 7

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

:rtype: (float, numpy.array(float))
Run Code Online (Sandbox Code Playgroud)