返回多个值的python docstring约定是什么?

2024-10-02 08:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我关心的是docstring中的:rtype:注释;如果一个函数返回多个值(如本例所示),那么:rtype:应该如何写入docstring(根据PEP-8)?在

通常对于只返回一个值的函数,:rtype:将写为如下内容

^{pr2}$

因为只有一个返回值,所以没有指定返回的变量名。在

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


Tags: andofthe函数numpyaddreturnnp

热门问题