Docstrings when nothing is returned

Ricky Robinson picture Ricky Robinson · Jan 5, 2015 · Viewed 13.7k times · Source

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?

Answer

user2555451 picture user2555451 · Jan 5, 2015

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