Learning Python with Advent of Code Walkthroughs

Dazbo's Advent of Code solutions, written in Python

The Python Journey - Assertion

Useful Links

Assertions in PythonExceptions

Page Contents

Assertions and Invariants

Overview

Assertions are a great way to check that your code is doing what you think it should be doing. We use assertions to test for invariants. This means checking a condition that we always believe should be true. Or to put it another way:

Use assertions to test for something that you believe should always be true. Or, to put it another way: to generate a runtime error whenever we hit a condition that we believe should never happen.

(Contrast exceptions, which should be used for conditions that might happen, and which generally need to be handled.)

In plain English, an assertion might go something like this:

“I assert that the variable called okay is always True

The general Python syntax is:

assert condition [, message]

So, for the example above:

assert okay, "We're not okay!"

If the assertion is True, then the program continues without displaying any message. If the assertion fails (i.e. the condition is False), then an AssertionError will be thrown. Unless the exception is caught, this will cause your program to terminate.

Let’s try it out:

okay = True
assert okay, "We're not okay!"
print("Finished")

Output:

Finished
okay = False
assert okay, "We're not okay!"
print("Finished")

Output:

Traceback (most recent call last):
  File "f:\Users\Darren\localdev\Python\Basic-Scripts\src\Exceptions\assertion_demo.py", line 2, in <module>
    assert okay, "We're not okay!"
AssertionError: We're not okay!

Handing AssertionError

Like any exception in Python, we can handle it programmatically:

okay = False

try:
    print("About to assert...")
    assert okay, "We're not okay!"
    print("If our assertion fails, this message won't print.")
except AssertionError as e:
    print(e)
    
print("Finished")

Output:

About to assert...
We're not okay!
Finished

Assertions as a Way to Comment Code

Assertions are helpful in checking our code is behaving as we expect. At the same time, they are a useful way to document our code. So think about where an assertion might be preferable over a normal comment.

Asserting Unreachable Conditions

Consider using assert False for any condition that you believe should never happen.

I’ll set up a trivial example:

validated_input = "c"
if validated_input == "y":
    print("Yes")
elif validated_input == "n":
    print("No")
else:
    assert False, "Looks like we did a poor job validating the input."

Pretend that the variable called validated_input was set by some sort of user input process. We believe this user input has validation in place, such that at this point in the code, the variable can only be set to "y" or to "n". But somehow our validation has failed, and the variable has been set to "c". This shouldn’t be possible! The assert statement helps us identify that this scenario has happened.

Output:

Traceback (most recent call last):
  File "f:\Users\Darren\localdev\Python\Basic-Scripts\src\Exceptions\assertion_demo.py", line 7, in <module>
    assert False, "Looks like we did a poor job validating the input."   
AssertionError: Looks like we did a poor job validating the input.  

This can save you a lot of time in debugging problems in your code!

Performance Considerations

Used judiciously, assertions should have no significant impact on your application’s performance. But there are some considerations:

Examples