docstring for a area function in python

To add a docstring for an area function in Python, you can follow the below example:

main.py
def area(length, breadth):
    """
    Calculate the area of a rectangle
    
    :param length: int or float, the length of the rectangle
    :param breadth: int or float, the breadth of the rectangle
    :return: int or float, the area of the rectangle
    
    Example:
    >>> area(2, 4)
    8
    """
    return length * breadth
334 chars
14 lines

In the above example, we have added a docstring that describes the function, its parameters, return value and an example usage. The docstring is enclosed in triple quotes and is the first thing that appears after the function definition. The parameter names, types and descriptions are specified using the :param tag and the return value is specified using the :return tag. The example usage is included as a comment in the docstring using the >>> notation, which is a special format recognized by Python's built-in doctest module. It is recommended to include examples in your docstrings whenever possible, as they can serve as a useful reference for users.

gistlibby LogSnag