Sub topic 4
Stepping through code: next, step, continue, return, until
This chapter assumes you can start a pdb session with breakpoint() or python -m pdb, read the (Pdb) prompt, and read a traceback. Once the debugger has paused your program, stepping lets you move forward one piece at a time and watch how state changes.
The five stepping commands
Each command resumes execution until a stopping condition is met. Then pdb pauses again and shows the next line to run.
| Command | Short form | What it does |
|---|---|---|
| `next` | `n` | Runs the current line. A function call on that line runs to completion without stopping inside it. |
| `step` | `s` | Runs the current line. If it calls a function, stops on that function's first line. |
| `return` | `r` | Runs until the current function is about to return, then stops. |
| `until` | `unt` | Runs until a line number greater than the current one is reached. In a loop, it runs to the end of the loop. |
| `continue` | `c` | Runs until the next breakpoint or until the program ends. |
Pressing Enter on an empty prompt repeats the last command. This is handy when you want to press n many times in a row.
Next versus step
The difference matters when a line contains a call. Use next when you trust the called function, and step when you suspect it.
def square(x):
result = x * x
return result
def total_of_squares(numbers):
total = 0
for n in numbers:
total += square(n)
return total
breakpoint()
print(total_of_squares([1, 2, 3]))When the session opens, the debugger shows the print line. Typing s enters total_of_squares. Typing n repeatedly walks through its lines. At total += square(n), n runs square in one move, while s stops at result = x * x inside square.
(Pdb) s
--Call--
> example.py(6)total_of_squares()
-> def total_of_squares(numbers):
(Pdb) n
> example.py(7)total_of_squares()
-> total = 0The --Call-- marker tells you that you entered a new function. A --Return-- marker shows the value a function is about to return.
Finishing a function with return
Suppose you stepped into square and realize the rest of it is not interesting. Type r. Pdb runs the remaining lines and stops at the --Return-- marker, so you can see the return value before control goes back to the caller. One more n brings you to the calling line.
Skipping loop iterations with until
In a loop, next moves back to the top of the loop on every pass. until does not go backward. When you reach the last line of the loop body, unt keeps running until the loop finishes and execution reaches a later line.
(Pdb) l
7 for n in numbers:
8 -> total += square(n)
9 return total
(Pdb) unt
> example.py(9)total_of_squares()
-> return totalYou can also pass a line number, as in unt 9, to run until that line is reached in the current frame.
A stepping strategy
Stepping line by line through a large program is slow. A more efficient pattern is:
- Place a breakpoint just before the suspect code.
- Use
nover calls you trust. - Use
sonly on the call you suspect. - Use
rwhen you have seen enough of a function. - Use
untto leave a loop once you understand an iteration.
Exercise
You are paused on the line total += square(n) inside a loop. You believe square is correct but want to see how total changes after the line runs. Which command do you use, and which command would you use instead to look inside square?
Show answer
Use n to run the whole line, including the call to square, and stop on the next line, where you can print total with p total. Use s instead to stop on the first line of square.
Exercise
You stepped into a function with s and now want to see its return value without stepping through every line. Which command do you use, and what does pdb print?
Show answer
Use r. Pdb runs to the end of the function and prints a --Return-- marker with the value that is about to be returned.