Lesson · Logging & Diagnostics
Debugging Tools
68 min readDebugging Tools
Debuggers let you pause execution and inspect live state. `breakpoint()`, `pdb`, IDE debuggers, and stack inspection are most effective when combined with a clear hypothesis about the failure.
Learning goals
- Call
breakpoint()at a suspicious line and explain that it delegates to the debugger hook configured by the environment, defaulting topdb. - Drive a paused program with the
pdbcommandswhere,step,next,returnandcontinue. - Read live state at a stop: locals, arguments, and the call stack in each frame, instead of guessing from output.
- Explain what differs between
step(enters a call) andnext(stays in the current frame) at the same line. - Name one debugging behavior that Python 3.14 expands, and say why version matters when a feature concerns asyncio introspection.
Mental model
A debugger is a controlled pause: you stop where a hypothesis says something should already be true, then compare that claim against the live locals and the call stack. The worked example places the stop inside the loop, so the same pause can be replayed for each pass with different values.
Mental model
read it top to bottom
breakpoint() at a suspicious line the program pauses here ↓ pdb takes the terminal where, list, p inspect: locals, arguments, stack ↓ step / next / return choose the movement granularity continue until the next stop or exit
Version note
Python 3.14 expands asyncio introspection and command-line debugging support. Runtime inspection capabilities can be version-sensitive.
Core idea
A debugger turns a running program into a place you can stand at a chosen moment, and that is what separates it from reading output: at a stop you see the actual locals and the actual stack instead of text a developer decided to print. pdb splits its vocabulary across those two needs, with state commands such as p, pp and where, and movement commands such as step, next and continue. breakpoint() is only the summoning mechanism, and because the hook is configurable, the same call works under a terminal debugger or an IDE one without editing the source.
breakpoint()
breakpoint() enters the configured debugger hook, which defaults to pdb. It is a lightweight way to stop at a suspicious line without hard-coding a specific debugger API.
pdb
pdb is the standard library's interactive debugger and the default target of breakpoint(). At a pause it accepts state commands such as where for the call stack and p for evaluating an expression in the current frame, plus movement commands such as step, next, return and continue. It can also launch a program directly, as in python -m pdb app.py, which stops before the first line runs.
Inspecting state
Inspecting state is the information that persists between operations in Debugging Tools. Make ownership explicit so you can predict whether two operations observe the same state or independent copies.
Stepping
Stepping moves a paused program forward in small, chosen increments instead of letting it run free. step executes the current line and enters any call it makes, next executes the line without descending into calls, return runs until the current function exits, and continue resumes until the next breakpoint or the end of the program.
Stack inspection
In Debugging Tools, Stack inspection names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
IDE debugger concepts
An editor should make the active interpreter visible and provide syntax, linting, debugging, and terminal access without hiding how Python itself runs. Keep environment selection explicit.
Asyncio task introspection tools in Python 3.14
In Debugging Tools, Asyncio task introspection tools in Python 3.14 names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Version-sensitive debugger and runtime inspection features
In Debugging Tools, Version-sensitive debugger and runtime inspection features names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Walkthrough
calculatekeeps a runningtotalwhile the loop visits eachvalue, so the pause inside the loop is reached once per element and the same expressions can be re-read with a newvalueeach time.breakpoint()sits beforetotal += value, so at the first stop forcalculate([1, 2, 3])the namevaluealready holds1whiletotalis still0;p valueandp totalconfirm that ordering.- The inline comment describes a real workflow: pressing
cat each stop turns the debugging session back into an ordinary run without editing the source. - Predict before running: the pair
(value, total)at the first stop, how many pauses a three-element list produces, and whatcalculatereturns after you continue through every stop.
Experiments to try
- At the first pause, read
p values,p valueandp total, then compare each with what you expected before starting the run. - Wrap the addition in a helper such as
def add(a, b): return a + band call it from the loop; at that line, compares(which entersadd) withn(which stays incalculate). - Replace the bare
breakpoint()withif value == 2: breakpoint()and confirm the program stops exactly once forcalculate([1, 2, 3]). - Run the script with the
PYTHONBREAKPOINTenvironment variable set to0and confirm the pause disappears without any change to the source.
Common mistakes
- Stepping without a hypothesis: wandering through a function collects observations without a claim to test, so nothing tells you which stop was the useful one.
- Leaving a bare
breakpoint()in code that might be committed: it is not imported, so a review that scans import lines will not catch it, and every interactive run of that path halts. - Reading the wrong frame:
p nameinspects the frame you are stopped in, not the caller's, so usewhereand move withupordownbefore concluding anything about another function's locals. - Treating debugger output as reproducible: the values came from one run with one input, so a finding worth keeping needs its setup written down or pinned by a test.
Practice: Easy
- Run
calculate([1, 2, 3])and write down the pair(value, total)shown at each of the three stops; then presscand check the returned total against your notes. - At a pause, type
whereand list the frames fromcalculateup to the module level; state which framep totalreads before anyupcommand. - Add a condition so the program pauses only on the last element,
if value == values[-1]: breakpoint(), and show that exactly one stop occurs.
Practice: Medium
- Reproduce the same session without touching the source: run
python -m pdb app.py, setb calculatebefore starting, andcthrough every visit of the loop. - Move the addition into
def add(a, b): return a + band classify the two movement commands at the call line: record the linesstops on versus the linenstops on. - Debug a failing call
calculate(["1", 2]): find the exact line that raises theTypeError, then check at the previous stop howp valuealready reveals the string argument.
Practice: Hard
- Design a stepping plan for a small recursive function: name the exact commands that prove the base case is reached and that prove each partial result is combined on the way back, then execute the plan and record every stop.
- Build a failure lab with three debugging traps: a
breakpoint()left in a helper that a loop calls, apcommand aimed at a name that lives in another frame, and a session begun withPYTHONBREAKPOINT=0that silently disables every stop. Explain what each trap reveals about the hook and about frames. - Run an asyncio program under the debugger, pause inside a task, and inspect the live state; note which asyncio introspection facilities the lesson attributes to Python 3.14 and why a debugging checklist should record the interpreter version.
Lesson checklist
- I can state what
breakpoint()delegates to and howPYTHONBREAKPOINTchanges where the pause goes. - I can read any local at a stop, such as
valueandtotalin the loop, and resume withc. - I can choose between
step,next,returnandcontinuefor the movement I need. - I can tell which frame a
pcommand inspects and navigate the stack withwhere,upanddown.
Remember
A debugger replaces guessing with observation at a chosen moment: stop where a claim should hold, read the live locals and the stack, then move one increment at a time until the picture matches or contradicts the hypothesis. `breakpoint()` is the portable way to summon that pause, `pdb` supplies the commands that control it, and Python 3.14 keeps expanding the introspection surface that surrounding tools build on.
Interview angles
What is the main purpose of **breakpoint()**?+
breakpoint() enters the configured debugger hook, which defaults to pdb. It is a lightweight way to stop at a suspicious line without hard-coding a specific debugger API.
What should you pay attention to when using **pdb**?+
pdb is the standard library's interactive debugger and the default target of breakpoint(). At a pause it reads state with commands such as p and where and moves the program with step, next, return and continue; python -m pdb app.py can also place a program under it from the start.
How does **Inspecting state** fit into this lesson?+
Inspecting state is the information that persists between operations in Debugging Tools. Make ownership explicit so you can predict whether two operations observe the same state or independent copies.
Why is a bare `breakpoint()` risky in code that might be committed, and how can one run disable it without editing the source?+
It needs no import, so a review that scans import lines will not catch it, and any interactive run of that path halts; setting the PYTHONBREAKPOINT environment variable to 0 makes every breakpoint() call a no-op for that run.