Python Glossary Русский

all Python 3Exceptionsexception

AssertionError

built-in exceptions

The condition of an assert statement was false: assert x > 0.

Syntax

raise AssertionError

assert is for checking your own assumptions, not for validating user input: running Python with -O strips every assert out of the bytecode and the check silently vanishes. The classic trap is assert (x > 0, 'message') — the parentheses build a non-empty tuple, which is always truthy, so the assertion can never fail. Drop them: assert x > 0, 'message'.

Examples

try:
    assert 1 == 2
except AssertionError as e:
    print(type(e).__name__)   # → AssertionError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy