Sub topic 13
A hypothesis-driven debugging workflow
This chapter assumes you can start a pdb session with breakpoint(), step with n, s, and c, print values with p, and read a traceback. Here you combine those commands into a method. Instead of stepping through code at random, you decide what you believe is wrong and use the debugger to test that belief.
Why a workflow helps
Stepping line by line through a long program is slow, and you can easily lose track of what you were looking for. A debugging session works better as a series of small experiments. Each experiment has a guess, a check, and a result.
Worked example
This function should return the average of the positive numbers in a list. For [4, -2, 6] the correct answer is 5.0. It prints something else.
def average_positive(values):
positives = [v for v in values if v > 0]
total = 0
for v in positives:
total += v
return total / len(values)
print(average_positive([4, -2, 6]))The output is 3.3333333333333335, so the result is wrong but nothing crashes.
Step 1: state a hypothesis
Write down one specific, testable claim. Here is a first guess: "The filter keeps the wrong items, so positives is wrong." A vague goal such as "see what happens" gives you nothing to confirm or reject.
Step 2: choose the cheapest check
Pause just before the return, where both the filter result and the sum already exist.
def average_positive(values):
positives = [v for v in values if v > 0]
total = 0
for v in positives:
total += v
breakpoint()
return total / len(values)
print(average_positive([4, -2, 6]))Step 3: inspect and decide
(Pdb) p positives
[4, 6]
(Pdb) p total
10Both values are correct, so the first hypothesis is rejected. This is progress, because you have ruled out the filter and the loop.
Step 4: revise and repeat
The next hypothesis is "the divisor is wrong." You can test it without leaving the session, because p evaluates any expression.
(Pdb) p len(values), len(positives)
(3, 2)
(Pdb) p total / len(positives)
5.0The check confirms the hypothesis. The code divides by every input item, not by the positive ones. Change len(values) to len(positives) and run the script again to verify the fix.
Habits that keep the loop fast
- Test one hypothesis per pause. If a check could confirm two different ideas, split it into two checks.
- Pause as close to the suspected line as you can, so there is little code to step through.
- Evaluate candidate fixes with
pbefore editing the file. - For a bug that appears only on some iterations, use a conditional breakpoint such as
b 5, v < 0. For a bug that appears only after many calls, use an ignore count. - If you cannot form a hypothesis, narrow the search. Put one pause point halfway through the code, check whether the state is already wrong there, and move the pause point toward the side that contains the bug.
Common mistakes
| Mistake | Effect | Better approach |
|---|---|---|
| Pressing `n` repeatedly with no goal | Time passes and you learn nothing | State what you are looking for first |
| Changing code before confirming the cause | You may hide the symptom and keep the bug | Confirm with `p` first, then edit |
| Trusting a value you did not print | You debug your assumptions | Print it and compare it to your expectation |
Exercise
In the worked example, suppose p positives had printed [4, -2, 6]. Which hypothesis would that confirm, and what would you inspect next?
Show answer
It would confirm that the filter is wrong, because -2 should have been removed. Next, check the filter condition itself, for example p [v > 0 for v in values], and look at the comparison in the list comprehension.