Function Docstrings

def squares_a_list(numerical_list):
    new_squared_list = list()
    
    for number in numerical_list:
        new_squared_list.append(number ** 2)
    
    return new_squared_list
404 image
404 image
string1 = """This is a string"""
type(string1)
str

The NumPy format includes 4 main sections:

  • A brief description of the function
  • Explaining the input Parameters
  • What the function Returns
  • Examples

NumPy Format

def squares_a_list(numerical_list):
    """
    Squared every element in a list.
    
    Parameters
    ----------
    numerical_list : list
        The list from which to calculate squared values 
        
    Returns
    -------
    list
        A new list containing the squared value of each of the elements from the input list 
        
    Examples
    --------
    >>> squares_a_list([1, 2, 3, 4])
    [1, 4, 9, 16]
    """
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
    return new_squared_list
def function_name(param1, param2):
    """The first line is a short description of the function.
    
    A paragraph describing in a bit more detail what the function
    does and what algorithms it uses and common use cases.
    
    Parameters
    ----------
    param1 : datatype
        A description of param1.
    param2 : datatype
        A longer description because maybe this requires
        more explanation, and we can use several lines.
    
    Returns
    -------
    datatype
        A description of the output, data types, and behaviors.
        Describe special cases and anything the user needs to know 
        to use the function.
    
    Examples
    --------
    >>> function_name(3, 8, -5)
    2.0
    """

Default Arguments

def exponent_a_list(numerical_list, exponent=2):
    """
    Creates a new list containing specified exponential values of the input list. 
    
    Parameters
    ----------
    numerical_list : list
        The list from which to calculate exponential values from
    exponent: int or float, optional
        The exponent value (the default is 2, which implies the square).
        
    Returns
    -------
    new_exponent_list : list
        A new list containing the exponential value specified of each 
        of the elements from the input list 
        
    Examples
    --------
    >>> exponent_a_list([1, 2, 3, 4])
    [1, 4, 9, 16]
    """

    new_exponent_list = list()
    
    for number in numerical_list:
        new_exponent_list.append(number ** exponent)
    
    return new_exponent_list

Side Effects

def function_name(param1, param2):
    """The first line is a short description of the function. 
    
    If your function includes side effects, explain it clearly here.
    
    
    Parameters
    ----------
    param1 : datatype
        A description of param1.
    
    .
    .
    .
    Etc.
    """

How to read a docstring

?function_name


For example, if we want the docstring for the function len():

?len
Signature: len(obj, /)
Docstring: Return the number of items in a container.
Type:      builtin_function_or_method
?squares_a_list
Signature: squares_a_list(numerical_list)
Docstring:
Squared every element in a list.

Parameters
----------
numerical_list : list
    The list from which to calculate squared values 

Returns
-------
list
    A new list containing the squared value of each of the elements from the input list 

Examples
--------
>>> squares_a_list([1, 2, 3, 4])
[1, 4, 9, 16]
File:      ~/<ipython-input-1-7e6e50ac7556>
Type:      function

Let’s apply what we learned!