liveBook · Manning

Breakpoint

As explained in Python How-To

A breakpoint is a designated execution point at which a program pauses so its state can be examined during debugging.

Need to inspect a function while it runs? Place a breakpoint at the suspected location. Execution pauses there and activates Python’s interactive debugger, allowing the program state and subsequent steps to be examined. section 13.2.1

Use the simplest practical forms:

  • Call the built-in breakpoint() function directly. section 13.2.1
  • Use import pdb; pdb.set_trace() when working with the underlying debugger interface or legacy code. section 13.2.1
  • At the (Pdb) prompt, press n to execute the current line and stop at the next one; press Enter or Return to repeat n. section 13.2.2
  • Use s instead of n when you need to step into a called operation, and l to list nearby source lines. section 13.5 section 13.2.2

Inspecting task creation

Place breakpoint() before the operation you want to inspect: def create_task(inject_bug: bool): breakpoint() task_text = obtain_text_data(inject_bug) title, urgency_text = task_text.split(",") urgency = int(urgency_text) return Task(title, urgency) Running the script opens a (Pdb) prompt and identifies the current function, line number, and next line to execute. Step through the parsing operations with n; when a failure occurs, inspect the nearby source with l. section 13.2.2 section 13.2.1

Listing 13.3 Creating a function containing a breakpoint (task_bebug.py)
from collections import namedtuple
Task = namedtuple("Task", "title urgency")                #1
 
def obtain_text_data(want_bad):
    text = "Laundry,3#" if want_bad else "Laundry,3"
    return text
 
def create_task(inject_bug: bool):
    breakpoint()                                          #2
    task_text = obtain_text_data(inject_bug)              #3
    title, urgency_text = task_text.split(",")
    urgency = int(urgency_text)
    task = Task(title, urgency)
    return task
 
 
if __name__ == "__main__":
    create_task(inject_bug=False)

Explore a complete script that creates task data, inserts a breakpoint, and provides an optional path for injecting a parsing bug. section 13.2.2

How Python How-To covers it

How deep it goes
Explains activation and line-by-line navigation with n, s, and l, but does not detail the debugger’s full command set.
Examples
Uses a task-creation script that parses text data and steps through obtain_text_data, splitting, integer conversion, and object construction.
What it assumes
Assumes familiarity with Python functions and running scripts from a command-line tool while introducing debugger activation from the basics.
What you can do after
Diagnose bugs by pausing execution, identifying the next statement, and stepping through function operations interactively.
How it connects
Connects breakpoints with tracebacks, interactive debugging, pdb, exceptions, and function execution.

Also explained in

Compare how 6 books explain breakpoint →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.