The assert Statement in Python

Modified: | Tags: Python, Error handling

In Python, the assert statement allows you to implement assertions for debugging purposes. When the specified expression evaluates to false, Python raises an AssertionError and halts the program.

Basic Usage of the assert Statement

The basic syntax of the assert statement is:

assert expression

If the expression evaluates to true, the program continues. If it evaluates to false, an AssertionError is raised and the program stops.

a = 100

assert a == 100

# assert a == 0
# AssertionError: 

Like if statements, assert evaluates any value based on its truthiness. For example, numeric 0 and empty sequences are considered false.

assert [0, 1, 2]

# assert []
# AssertionError: 

You can combine multiple conditions using and or or, and negate them with not. More details on boolean operations will be discussed later.

a = 100

assert a > 0 and a % 2 == 0

# assert a < 0 and a % 2 == 0
# AssertionError: 

Add Error Messages to assert

You can optionally provide a second expression to assert, separated by a comma:

assert expression1, expression2

When expression1 evaluates to false, expression2 is passed as an argument to AssertionError. This allows you to include a custom error message when the assertion fails.

a = 100

# assert a == 0, 'a must be 0.'
# AssertionError: a must be 0.

Set Multiple Conditions in assert

While you can use and to combine multiple conditions, be aware that due to short-circuit evaluation, the right side of and won't be evaluated if the left side is false.

def test1(x):
    print('test1 is called.')
    return x > 0

def test2(x):
    print('test2 is called.')
    return x % 2 == 0

a = -100

# assert test1(a) and test2(a), 'Error Message'
# test1 is called.
# AssertionError: Error Message

If you use separate assert statements, the program will halt at the first failed assertion:

# assert test1(a), 'Error Message1'
# assert test2(a), 'Error Message2'
# test1 is called.
# AssertionError: Error Message1

To ensure that all conditions are evaluated regardless of the outcome, you can use a tuple comparison:

# assert (test1(a), test2(a)) == (True, True), 'Error Message'
# test1 is called.
# test2 is called.
# AssertionError: Error Message

Disable assert with -O or -OO Options

According to the official documentation, assert statements (assert expression or assert expression1, expression2) are equivalent to the following code:

if __debug__:
    if not expression: raise AssertionError
if __debug__:
    if not expression1: raise AssertionError(expression2)

The __debug__ constant is True by default, but becomes False when Python is run with the -O or -OO optimization options.

When __debug__ is False, all assert statements are effectively disabled. This means that running Python with python -O script.py will skip all assertion checks.

Keep in mind that assert statements are intended for debugging purposes only. For runtime condition checking, use proper exception handling instead.

Related Categories

Related Articles