Next / Previous / Contents / TCC Help System / NM Tech homepage

22.2. The assert statement: Verify preconditions

To check for “shouldn't happen” errors, you can use an assert statement:

assert e1
assert e1, e2

where e1 is some condition that should be true. If the condition is false, Python raises an AssertionError exception (see Section 25, “Exceptions: Error signaling and handling”).

If a second expression e2 is provided, the value of that expression is passed with the exception.

Assertion checking can be disabled by running Python with the -O (optimize) option.