The assert Statement
The assert statement verifies that a condition is true; if false, it raises an AssertionError
A Checkpoint for Assumptions
Programs often rely on assumptions about their current state. For example, you may be certain that a list has at least one element before the next operation uses it. The assert statement lets you verify that assumption explicitly. If the condition is true, execution continues silently. If it is false, Python raises an AssertionError immediately.
The Assertion Check
An assert statement has two required parts: the keyword assert and a condition. The condition can be any boolean expression. Python evaluates that condition when it reaches the assertion. A true condition makes the assertion pass silently, so execution moves to the next line. A false condition causes Python to raise an AssertionError, and execution halts at that point.
In this example, the condition checks the assumption that items contains at least one element. Because the condition is true, the assertion produces no output and execution reaches the print statement.
reportTracing a Failed Assertion
What do you think happens?
What happens when execution reaches this assertion?
Reveal answer
Answer: The assertion fails and the final print does not run
The condition is false because the list is empty. Python immediately raises an AssertionError, so execution stops at the assertion.
items = [] print("before check") assert len(items) > 0 print("after check")
To locate the failure, find the assertion that Python reached and then inspect the condition it evaluated. The line immediately before the failed assertion has already executed. The line after it has not, because the AssertionError stops execution at the assertion.
Adding Useful Failure Context
A custom assertion message is added after the condition with a comma. If the condition is false, the message is displayed with the AssertionError, making the failed assumption and its intended purpose more informative.
Both forms verify a condition in the same way. The difference is the information available when the condition fails: the form without a message raises an AssertionError without the custom explanation, while the form with a message adds context about what went wrong.
Choosing the Right Role
Use assertions as a development and debugging tool for verifying assumptions about program state. They are useful when you believe a condition should always be true and want the program to identify the exact point where that assumption breaks down.
| Situation | Appropriate role for assert |
|---|---|
| Verifying an assumption during development | Use assert to check that the condition is true. |
| Handling an expected runtime error | Do not use assert as the error-handling mechanism. |
| Production-critical logic | Do not rely on assert because assertions can be disabled with Python's -O flag. |
Mistakes to Avoid
Assuming a failed assertion lets the next line run.
A false condition raises AssertionError immediately and halts execution at the assertion.
Fix:
Treat the assertion as the stopping point when its condition is false.Using assert to handle an expected runtime error.
Assertions are a development and debugging tool, not a mechanism for handling expected runtime errors.
Fix:
Use ordinary program logic for expected runtime situations instead of relying on an assertion.Relying on an assertion for production-critical behavior.
Python can disable assertions with the -O flag.
Fix:
Keep critical logic independent of assertions.Providing no context for an important failed assumption.
The failure gives less information about why the condition mattered.
Fix:
Add a comma and a custom message when extra explanation will help diagnose the failure.
Practice the Trace
Consider this sequence: a program prints a message, reaches an assert statement whose condition is false, and then contains another print statement. Identify which output appears, where the AssertionError occurs, and whether the later print statement runs. Then rewrite the assertion with a custom message that explains the assumption being checked.
Hints
- A true assertion continues silently, but a false assertion raises AssertionError.
- The statement after a failed assertion is not reached.
- Place the custom message after the condition and a comma.
- The assert statement verifies that a condition is true. Python evaluates the condition when execution reaches the assertion. A true condition passes silently and execution continues; a false condition raises AssertionError and stops execution at that point. Add a comma followed by a message when a failure needs more context. Use assertions for development and debugging assumptions, not for expected runtime errors or production-critical logic.
Key Takeaways
- assert checks whether a condition is true.
- A true assertion passes silently and execution continues.
- A false assertion raises AssertionError and halts execution at the assertion.
- A custom message after a comma makes a failed assertion more informative.
- Assertions support development and debugging, but should not handle expected runtime errors or production-critical logic.