write a docstring in python in python

To write a docstring in Python:

  1. Start with triple quotes - either """ or '''.
  2. Write a description of what the function or module does.
  3. Document the parameters and return values, if applicable.
  4. Add any examples or notes that might be helpful.

Here's an example of a function with a docstring:

main.py
def greet(name):
    """Return a greeting for the given name.

    Parameters:
    name (str): The name to be greeted.

    Returns:
    str: The greeting.

    Example:
    >>> greet("Alice")
    'Hello, Alice!'
    """

    return f"Hello, {name}!"
251 chars
16 lines

Note that the docstring is accessed via the __doc__ attribute of the function or module, so it's important to keep the docstring formatted consistently with the rest of the code.

related categories

gistlibby LogSnag