Skip to content

Assert Statement in Python

diagram assert disappears under -O, so it cannot be a check mermaid
An assert is a statement the interpreter is allowed to remove. Run with optimisations and every one of them is gone -- the condition is not evaluated and the body it was guarding runs anyway. That makes it a tool for catching your own logic errors during development, and the wrong tool for validating anything that comes from outside.

Asserting Excellence: A Comprehensive Guide to the assert Statement in Python

Section titled “Asserting Excellence: A Comprehensive Guide to the assert Statement in Python”

In Python, the assert statement is a powerful tool for debugging and ensuring that certain conditions hold true during the execution of a program. It allows developers to express assumptions about the state of the code and halts program execution if these assumptions are not met. In this comprehensive guide, we’ll explore the syntax, use cases, and best practices associated with the assert statement in Python.

The assert statement has a simple syntax:

Syntax
assert expression [, message]
  • expression: A condition that should evaluate to True. If it evaluates to False, the assert statement raises an AssertionError.
  • message (optional): An additional message that is displayed when the assertion fails.
assert_statement.py
x = 5
assert x > 0, "x should be a positive number"

Output:

command
C:\Users\Your Name> python assert_statement.py
Traceback (most recent call last):
  File "assert_statement.py", line 3, in <module>
    assert x > 0, "x should be a positive number"
AssertionError: x should be a positive number

In this example, if the value of x is not greater than 0, an AssertionError is raised with the specified message.

During the development phase, the assert statement is a valuable tool for catching logical errors early in the code. It allows developers to express their assumptions about the code’s state and automatically checks if those assumptions hold true.

assert_statement.py
def calculate_discount(price, discount_rate):
    assert 0 <= discount_rate <= 1, "Discount rate should be between 0 and 1"
    # Rest of the function code

Output:

command
C:\Users\Your Name> python assert_statement.py
Traceback (most recent call last):
  File "assert_statement.py", line 2, in <module>
    assert 0 <= discount_rate <= 1, "Discount rate should be between 0 and 1"
AssertionError: Discount rate should be between 0 and 1

Here, the assert statement ensures that the discount rate is within a valid range, providing an early indication if it’s not.

In unit testing and quality assurance processes, the assert statement is used to verify that the code behaves as expected. It helps in creating test cases and asserting that certain conditions are met during the execution of the code.

assert_statement.py
def divide(a, b):
    assert b != 0, "Cannot divide by zero"
    return a / b
 
print(divide(10, 0))

Output:

command
C:\Users\Your Name> python assert_statement.py
Traceback (most recent call last):
  File "assert_statement.py", line 4, in <module>
    print(divide(10, 0))
  File "assert_statement.py", line 2, in divide
    assert b != 0, "Cannot divide by zero"
AssertionError: Cannot divide by zero

This assert statement ensures that attempting to divide by zero will result in an AssertionError during testing.

The assert statement serves as a form of documentation by explicitly stating assumptions about the code. When used judiciously, it can make the code more understandable and help other developers grasp the intended behavior.

assert_statement.py
def process_data(data):
    assert len(data) > 0, "Input data should not be empty"
    # Rest of the function code
 
process_data([])

Output:

command
C:\Users\Your Name> python assert_statement.py
Traceback (most recent call last):
  File "assert_statement.py", line 4, in <module>
    process_data([])
  File "assert_statement.py", line 2, in process_data
    assert len(data) > 0, "Input data should not be empty"
AssertionError: Input data should not be empty

This assert statement communicates the expectation that the input data should not be empty.

Best Practices for Using the assert Statement

Section titled “Best Practices for Using the assert Statement”

It’s essential to keep in mind that the assert statement should not have side effects. The purpose of assert is to check conditions, not to modify the program’s state.

assert_side_effects.py
# Avoid
assert x > 0, x = 0
 
# Prefer
assert x > 0, "x should be a positive number"

Output:

command
C:\Users\Your Name> python assert_side_effects.py
Traceback (most recent call last):
  File "assert_side_effects.py", line 2, in <module>
    assert x > 0, x = 0
AssertionError: 0

In this example, the assert statement has a side effect of modifying the value of x to 0. This is not recommended and can lead to unexpected behavior. Instead, the assert statement should be used to check the condition and raise an AssertionError if it’s not met. The assert statement should not modify the value of x.

While assert can be useful for catching bugs, it is not intended for data validation in production code. It can be disabled globally, and relying on it for input validation might introduce security vulnerabilities.

When using the assert statement, provide clear and informative messages. These messages serve as documentation and aid in understanding the cause of the failure when an assertion error occurs.

assert_message.py
assert len(data) > 0, "Input data should not be empty"

Output:

command
C:\Users\Your Name> python assert_message.py
Traceback (most recent call last):
  File "assert_message.py", line 1, in <module>
    assert len(data) > 0, "Input data should not be empty"
AssertionError: Input data should not be empty

In this example, the message “Input data should not be empty” provides additional context about the cause of the assertion error.

4. Use Conditional Statements for Production Code:

Section titled “4. Use Conditional Statements for Production Code:”

For conditions that are critical for the correctness of the program and should be checked even in production, consider using conditional statements (e.g., if, raise) rather than assert.

assert_condition.py
if x <= 0:
    raise ValueError("x should be a positive number")

Output:

command
C:\Users\Your Name> python assert_condition.py
Traceback (most recent call last):
  File "assert_condition.py", line 2, in <module>
    raise ValueError("x should be a positive number")
ValueError: x should be a positive number

In this example, the raise statement is used to raise a ValueError exception if the condition is not met. This is preferred over using assert because it ensures that the condition is checked even in production.

The assert statement in Python is a powerful tool for expressing and validating assumptions about the state of the code. While it is invaluable during development, testing, and debugging, it should be used judiciously and with careful consideration of its limitations. By following best practices and providing clear messages, the assert statement contributes to the reliability and maintainability of Python code.

As you advance in your Python programming journey, explore the effective use of the assert statement to catch potential issues early and create more robust and dependable software. For more insights and practical examples, check out our tutorials on Python Central Hub!

Section titled “As you advance in your Python programming journey, explore the effective use of the assert statement to catch potential issues early and create more robust and dependable software. For more insights and practical examples, check out our tutorials on Python Central Hub!”
sketch assert disappears under -O, and so does everything inside it p5.js
An assert is a statement the interpreter is allowed to remove. Run with optimisations and the whole line is gone -- the condition is never evaluated, so any side effect in it vanishes too. That is what makes it a tool for catching your own logic errors and the wrong tool for checking anything that came from outside.
pch.quizTag pch.quizDefaultTitle
  1. What does `python -O script.py` do to `assert` statements?

    pch.quizShowAnswer

    B — Removes them entirely — the condition is never evaluated — `__debug__` becomes False and the whole statement is stripped at compile time. Any side effect inside the condition disappears with it.

  2. Why should you never validate user input with `assert`?

    pch.quizShowAnswer

    B — It can be removed by `-O`, so the check may not run at all — A validation that vanishes under an interpreter flag is not a validation. Raise a real exception — `ValueError`, or a custom type — for anything that comes from outside.

  3. What is `assert` genuinely for?

    pch.quizShowAnswer

    B — Stating an invariant you believe is always true, to catch your own logic errors — It documents an assumption and fails loudly during development if the assumption breaks. It is a statement about your code, not about your data.

  4. What is wrong with `assert (x > 0, 'x must be positive')`?

    pch.quizShowAnswer

    B — It asserts a non-empty TUPLE, which is always truthy — so it never fails — The comma makes it one tuple argument rather than a condition and a message. A non-empty tuple is always truthy, so the assertion can never fire — modern Python warns about this.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading