liveBook · Manning

Docstring

As explained in A Pythonic Adventure: From Python basics to a working web app

A docstring is a string literal associated with a Python module, class, or function that documents its purpose and behavior.

A docstring is a special string placed in a Python function to document its purpose and behavior. Unlike a quick comment, it can become part of the function’s documentation. section 14.0

Think of it as leaving a helpful note for your future self—or for someone else reading your code months later. Keep explaining original or complicated code yourself, even when AI can suggest comments. section 14.0

Documenting a menu reader

The function’s docstring states what read_menu does, names its filename parameter, describes the returned list, and notes that surrounding whitespace is removed. section 14.0

def read_menu(filename):
    """
    Reads the menu from a file and returns a list of items.
 
    :param filename: The name of the file containing the menu.
    :return: A list of menu items, with leading and trailing
    whitespaces removed.
    """
    try:
        # Open the file using 'with' to ensure it's properly
        # closed after use
        with open(filename) as f:
            # Read all lines from the file and remove whitespaces
            # using list comprehension
            result = [item.strip() for item in f.readlines()]
    except FileNotFoundError:
        # Handle the case when the file does not exist
        print(f"Error: The file '{filename}' could not be found.")
        result = []
    except PermissionError:
        # Handle the case when there is no permission to read
        # the file
        print(f"Error: You do not have permission to read \
        the file '{filename}'.")
        result = []
    except Exception as e:
        # Handle any other unexpected errors
        print(f"An unexpected error occurred: {e}")
        result = []
 
    return result

Inspect a complete docstring alongside comments explaining file handling and exception cases. section 14.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Defines docstrings and demonstrates parameter and return descriptions, but stops short of deeper documentation mechanics.
Examples
Demonstrates a read_menu function documenting its filename parameter, returned menu items, and whitespace removal.
What it assumes
Assumes familiarity with Python functions and comments while treating docstrings as a new documentation practice.
What you can do after
Apply it in Python by adding a clear purpose, parameter description, and return description to a function.
How it connects
Connects docstrings with comments, functions, exception handling, and code documentation.

Also explained in

Compare how 6 books explain docstring →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.