2025, Nov 06 11:00
Debugging Loops in Python Pdb: Auto-display a Variable’s Value at Every Breakpoint Hit
Learn how to use Pdb’s display command to automatically show loop variables at breakpoints. Cut manual prints, reduce noise, and spot changes faster in Python.
When you debug Python with Pdb and need to inspect how a variable evolves inside a loop, repeatedly hitting continue and typing print quickly becomes mechanical busywork. The flow is interrupted, the session is noisy, and you still risk missing a change. There is a cleaner way to surface values on every breakpoint hit without manual print calls.
Problem setup
Consider a simple script where values from a list are processed in a loop. The goal is to watch the loop variable each time execution pauses at a breakpoint in the loop body.
vals = [12, 3, 4, 10, 23, 1]
def mul_twice(arg):
return 2 * arg
for item in vals:
outcome = mul_twice(item)
Setting a breakpoint on the assignment inside the loop and then printing the variable on every stop works, but it’s repetitive and easy to get wrong under time pressure.
What’s really going on
Pdb stops at the breakpoint exactly where you asked, but it won’t display anything unless you explicitly request it. Using print after every continue is a manual inspection loop you have to run yourself. The need is clear: a way to have the debugger automatically show a variable at each stop without additional commands.
Solution: use Pdb’s display
Pdb has a built-in command for this use case. Tell the debugger what you want to observe once, and it will show the value automatically from then on. The command is straightforward:
(Pdb) display variable
With this in place, the debugger prints the current and previous values of that variable every time it stops. Below is how a session looks when tracking a loop variable at a breakpoint:
$ python -m pdb program.py
> /tmp/program.py(1)<module>()
-> a = [12, 3, 4, 10, 23, 1]
(Pdb) b 9
Breakpoint 1 at /tmp/program.py:9
(Pdb) c
> /tmp/program.py(9)<module>()
-> b = compute(i)
(Pdb) display i
display i: 12
(Pdb) c
> /tmp/program.py(9)<module>()
-> b = compute(i)
display i: 3 [old: 12]
(Pdb) c
> /tmp/program.py(9)<module>()
-> b = compute(i)
display i: 4 [old: 3]
(Pdb) c
> /tmp/program.py(9)<module>()
-> b = compute(i)
display i: 10 [old: 4]
(Pdb) c
> /tmp/program.py(9)<module>()
-> b = compute(i)
display i: 23 [old: 10]
Once configured, the value appears on every stop without extra input, making iteration-by-iteration inspection much smoother.
Why this matters
Automatic display eliminates repetitive commands, reduces cognitive load, and keeps focus on the signal rather than the mechanics of the debugging session. Seeing both the current and previous values makes it easy to spot unexpected transitions as you step through breakpoints.
Takeaways
If you’re watching a variable across breakpoint hits in Pdb, set a single display and let the debugger do the rest. Place the breakpoint at the line that matters inside the loop, use display for the variable of interest, and iterate with continue. This keeps your session concise and your attention on the behavior you’re investigating.
The article is based on a question from StackOverflow by Hamid Rouhani and an answer by Hamid Rouhani.