pdb Fundamentals
pdb Fundamentals
Drop into an interactive session with the builtin (Python 3.7+):
def total(rows):
s = 0
for r in rows:
breakpoint() # pauses here, no import needed
s += r["amount"]
return s
Commands at the (Pdb) prompt:
| Cmd | Action |
|---|---|
n |
execute next line in this frame |
s |
step into a called function |
c |
continue to next breakpoint |
r |
run until the current function returns |
p expr / a |
print expression / show args |
w / u / d |
backtrace, move up/down frames |
l / q |
list source, quit |
Post-mortem on an exception: python -m pdb script.py, then c — pdb stops at the raise point with full locals.
Breakpoints, ipdb, and Tests
Breakpoints, ipdb, and Tests
Environment variables redirect or silence the builtin:
PYTHONBREAKPOINT=0 python app.py # disable all breakpoint() calls
PYTHONBREAKPOINT=ipdb.set_trace python app.py # richer REPL with completion
Set conditional breakpoints in code (if attempt == 3: breakpoint()) or from the pdb prompt with b file.py:42, condition / b func, expr.
For tests, pytest --pdb opens the debugger at the first failing assert; pair it with --tb=short for readable output. Watch one habit: inspect with p, then continue — mutating objects while exploring silently changes program state and can hide the very bug you are chasing.
Pitfalls and Interview Angles
Pitfalls and Interview Angles
Print-debugging fails for concurrency. Interleaved async tasks or threads scramble print order; logging with task IDs or a debugger with per-frame inspection stays coherent.
Read tracebacks bottom-up. The last line is the actual error; frames above show the call chain. Chained exceptions (raise X from Y) link causes — inspect __cause__ instead of guessing.
Debugger vs production. Breakpoints freeze one process; in production use structured logging, faulthandler, or remote profilers.
Interview angle: describe a debugging loop — reproduce deterministically, form a hypothesis, place one breakpoint or log line to test it, confirm, remove the probe — and name the pdb command you would use to inspect a value in a parent frame (w, then u, then p).
Quiz
1. Which pdb command executes the current line and steps into a function call?
2. What does PYTHONBREAKPOINT=0 do?
3. Where in a traceback is the root error described?
Flashcards
Question
breakpoint()
Click to reveal answer
Answer
Built-in (3.7+) that starts pdb — or whatever PYTHONBREAKPOINT points to; 0 disables it.
Question
n vs s in pdb
Click to reveal answer
Answer
n runs the next line in place (skips into); s steps into called functions.
Question
Post-mortem debugging
Click to reveal answer
Answer
python -m pdb script.py then c — stops at the raise with locals intact.
Question
pytest --pdb
Click to reveal answer
Answer
Opens the debugger at the first failing test for interactive inspection.
Revision Notes
Key Takeaways
- 1. breakpoint() plus a handful of pdb commands covers most interactive debugging
- 2. Gate probes with PYTHONBREAKPOINT=0 and prefer logging over prints in concurrent code
- 3. Read tracebacks bottom-up and follow __cause__ through chained exceptions
Interview Tips
- • Name specific commands (n, s, u, p, w) when asked how you debug, not just "I use a debugger"
- • Frame debugging as hypothesis testing with a minimal reproduction to show disciplined troubleshooting
Cheat Sheet
breakpoint() → pdb: n/s/c/r/p/w/u/d/l/b; PYTHONBREAKPOINT=0 disables; pytest --pdb on failure; traceback: last line = error, frames above.
Continue Learning
Before You Start
- No prerequisites listed.