Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document multiple return values using reStructuredText in Python 2?

The Python docs say that "the markup used for the Python documentation is reStructuredText". My question is: How is a block comment supposed to be written to show multiple return values?

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?
    """

I've found a tutorial on Python documentation using reStructuredText, but it doesn't have an example for documenting multiple return values. The Sphinx docs on domains talks about returns and rtype but doesn't talk about multiple return values.

like image 426
Marty Chang Avatar asked Sep 29 '16 00:09

Marty Chang


People also ask

How do you return multiple returns in Python?

Return multiple values using commas In Python, you can return multiple values by simply return them separated by commas. In Python, comma-separated values are considered tuples without parentheses, except where required by syntax.

Can we return multiple values from a function in Python?

You can return multiple values from a function in Python. To do so, return a data structure that contains multiple values, like a list containing the number of miles to run each week. Data structures in Python are used to store collections of data, which can be returned from functions.

How do you return multiple values from a dictionary in Python?

To return multiple values from a Python function, use a comma to separate the return values.

Which allows to return multiple values from a function in Python?

Using Comma-separated values (Tuples): In this method, we use Python to return multiple values by simply separating them by commas. Python basically uses a tuple to achieve this.


1 Answers

There is a compromised solution: just write in normal Markdown texts. e.g.

def func(a, b):
    """

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

    return x, y

This will generate the following document:

enter image description here

Almost what we want, right?

The shortcoming for this is that you cannot specify return type for every element. You would have to write it by yourself, such as

"""
:returns:
    -x (:py:class:`int`) - first output
"""
like image 164
ComeOnGetMe Avatar answered Sep 25 '22 07:09

ComeOnGetMe