Debugging
Reproduce a failure, inspect state, and verify the correction.
On this page
The concept
A logical error can produce the wrong answer without raising an exception. Debugging begins with a small reproducible example and a written expected result. Compare it with the actual result, form one hypothesis, and change one thing at a time.
A print call can expose an intermediate value. The interactive debugger can pause execution with breakpoint(); in the debugger, n advances, p inspects, c continues, and q quits. That exploratory call belongs in your temporary working copy. The distributed example remains noninteractive so automated checks cannot hang.
Worked example
def is_even(number):
return number % 2 == 0
assert is_even(2) is True
assert is_even(3) is False
assert is_even(0) is True
assert is_even(-4) is True
price = 100
discount = 0.2
final_price = price * (1 - discount)
assert final_price == 80
print("All checks passed.")Trace the result
- The parity assertions include zero and a negative even number, not only an obvious positive case.
- The discount calculation has an independently known expected result of 80.
- Successful completion prints a final message; a failed assertion would stop at its failing location.
All checks passed.Line-by-line walkthrough
Lines 1–2: def is_even(number):
Define the function is_even with input names number. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.
Line 2: return number % 2 == 0
Evaluate number % 2 == 0 and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.
Line 5: assert is_even(2) is True
Check is_even(2) is True and raise AssertionError if it is false. This is a test/internal assumption; optimized Python may remove assertions, so it must not enforce untrusted-input rules.
Line 6: assert is_even(3) is False
Check is_even(3) is False and raise AssertionError if it is false. This is a test/internal assumption; optimized Python may remove assertions, so it must not enforce untrusted-input rules.
Line 7: assert is_even(0) is True
Check is_even(0) is True and raise AssertionError if it is false. This is a test/internal assumption; optimized Python may remove assertions, so it must not enforce untrusted-input rules.
Line 8: assert is_even(-4) is True
Check is_even(-4) is True and raise AssertionError if it is false. This is a test/internal assumption; optimized Python may remove assertions, so it must not enforce untrusted-input rules.
Line 9: price = 100
Evaluate 100, then bind or update price with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 10: discount = 0.2
Evaluate 0.2, then bind or update discount with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 11: final_price = price * (1 - discount)
Evaluate price * (1 - discount), then bind or update final_price with the result. Assignment itself does not imply a deep copy.
Line 12: assert final_price == 80
Check final_price == 80 and raise AssertionError if it is false. This is a test/internal assumption; optimized Python may remove assertions, so it must not enforce untrusted-input rules.
Line 13: print("All checks passed.")
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Adding a broad exception handler to suppress a failure does not repair its cause. Inspect the value that first becomes incorrect.