Sub topic 5
Inspecting variables and source: p, pp, args, list, display
This chapter assumes you can start a pdb session with breakpoint() and move with n, s, and c. It covers the commands that show you what the program holds at the current line.
Printing values with p and pp
p expression evaluates any Python expression in the current frame and prints its repr(). pp expression does the same through pprint, which wraps long structures across lines.
Here is a script with a bug. The discount is applied to the wrong items.
def apply_discount(items, rate):
result = {}
for name, price in items.items():
if price > 50:
result[name] = price * (1 - rate)
else:
result[name] = price
breakpoint()
return result
order = {"book": 20, "lamp": 80, "desk": 250, "pen": 2}
print(apply_discount(order, 0.1))At the prompt, try these commands:
(Pdb) p rate
(Pdb) p len(items)
(Pdb) p items["lamp"] * (1 - rate)
(Pdb) pp locals()Because p accepts any expression, you can test an idea without editing the file. Use pp for nested dictionaries and long lists, where p prints one unreadable line.
A line that is not a pdb command is run as Python. If a variable has the same name as a command, such as n or c, pdb treats it as the command. Write p n to print the variable.
Showing arguments with args
a (or args) prints the argument list of the current function with each argument's current value. At the breakpoint above it shows items and rate. Arguments can be rebound inside a function, so the values may differ from what the caller passed.
Reading source with list and ll
l (or list) shows 11 lines around the current line, and an arrow -> marks the line that runs next. Repeating l continues from where the last listing ended. l . returns to the current line. l 1, 10 shows an explicit range.
ll (or longlist) shows the whole source of the current function. Use it when you have lost your place after several steps.
Watching values with display
Typing p total after every step is tedious. display expression prints the expression each time execution stops in the current frame, but only when the value has changed.
(Pdb) display result
(Pdb) display len(result)
(Pdb) ndisplay with no argument lists the active expressions. undisplay 1 removes the first one, and undisplay with no number removes all of them.
Common mistakes
- Using
pon a generator or iterator consumes it, which changes program behavior. Inspect it withp list(...)only on a copy. - Calling a function with side effects from
pruns that side effect for real. - Reading
loutput as if the arrow line has already run. It has not.
Exercise
You are stepping through a loop with n and want to see total after every step without typing a command each time. Which command do you use, and when does it print?
Show answer
Run display total. pdb then prints total each time execution stops in the current frame, and only when its value has changed since the last display.
Exercise
A variable in your function is named c. How do you print its value, and why does c alone not work?
Show answer
Use p c. A bare c is the pdb command continue, so it resumes the program instead of showing the variable.