Search code examples
pythonpep8docstring

What is the python docstring convention for returning multiple values?


Say I have a function that returns more than one value

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

My concern is the :rtype: comment in the docstring; if a function returns multiple values, as this example does, how should the :rtype: be written in the docstring (according to PEP-8)?

Usually for functions that return only one value, the :rtype: would be written something like

"""
...

:rtype: int
"""

where the returned variable name is not specified as it doesn't matter, because there is only a single return value.

I understand that ideally I should try to break up my functions into simpler functions, which is certainly possible for add_and_raisepower above, however I only use this as a toy example to illustrate the question.


Solution

  • The result is a tuple every times in this case. Write it down like this:

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