Docstrings when nothing is returned

Question:

What is the docstring convention when a function doesn’t return anything?

For example:

def f(x):
    """Prints the element given as input

    Args:
        x: any element
    Returns:
    """
    print "your input is %s" % x
    return

What should I add after Returns: in the docstring? Nothing as it is now?

Asked By: Ricky Robinson

||

Answers:

You should use None, as that is what your function actually returns:

"""Prints the element given as input

Args:
    x: any element
Returns:
    None
"""

All functions in Python return something. If you do not explicitly return a value, then they will return None by default:

>>> def func():
...     return
...
>>> print func()
None
>>>
Answered By: user2555451

You can simply omit it. And for simplicity and reduced noise you probably should omit it.

The docstring style you used is "Google Style" and the style guide says this about the Returns section:

If the function only returns None, this section is not required.

https://google.github.io/styleguide/pyguide.html#doc-function-returns

Answered By: bugmenot123
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.