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.
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.
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.
To return multiple values from a Python function, use a comma to separate the return values.
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.
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:
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
"""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With