4.2 Breakpoints, Variables, and Single-Step Execution
Prerequisites: Prepare a Debuggable Development Environment
Estimated time: 12 minutes
Exercise environment: Python 3 and a selected debugger
You are ready to continue when: you can reproduce a failure, pause execution at an executable line, and choose the correct single-step action
The environment is aligned, and it's now time to address business errors. Save the following minimal failure example as material_checker.py:
def get_qualified_items(items):
# Requirement: zero-weight placeholders are valid; negative weights are not
valid = []
for item in items:
if item["weight"] > 0:
valid.append(item)
return sorted(valid, key=lambda item: item["weight"], reverse=True)
items = [
{"id": 1, "weight": 100},
{"id": 2, "weight": 0},
{"id": 3, "weight": -50},
]
assert [item["id"] for item in get_qualified_items(items)] == [1, 2]Confirm the assertion is reliably failing using python3 material_checker.py, then set a breakpoint at the line indicated by if item["weight"] > 0. The code is intentionally written as a regular loop to make item more observable in common Python debuggers. Breakpoints typically pause execution before the source code step is executed, but the source code line and the actual steps executed by the interpreter are not guaranteed to align one-to-one.
What to Look At After a Pause
- Which function, thread, and source file does the current stack frame belong to?
- Is the parameter
itemsthe expected input? - What is the result of the zero-weight item calculation
item["weight"] > 0? - Are the current interpreter, working directory, and loaded code correct?
Single-Step Actions
| Action | Behavior |
|---|---|
| Continue | Run until the next pause reason or end |
| Step Over | Execute the current step without entering called functions actively |
| Step Into | Attempt to enter the call, which may lead into library code |
| Step Out | Continue running the current function until returning to the caller vicinity |
Keyboard shortcuts vary depending on the IDE, operating system, and keybinding scheme. In VS Code, common keys are F5/F10/F11/Shift+F11, but these are just defaults and not universal across tools.
This example assumes every input has a numeric weight; the assertion does not cover missing fields, strings, or NaN. For the supplied case, change the condition to if item["weight"] >= 0:. After fixing, exit the debugger mode and run python3 material_checker.py. Debugging observations help form hypotheses, but testing is what proves that this requirement example has passed.
You can temporarily insert breakpoint() into Python code; the default configuration usually enters pdb, but PYTHONBREAKPOINT can change or disable this behavior. It alters the execution flow, so it should be cleaned up before submission or deployment, to avoid accidentally stopping unattended processes.