DEV Community

Favour George
Favour George

Posted on • Originally published at psycode.hashnode.dev on

Python’s Assert Statement: A Not-So Comprehensive Guide

What are Assertions?

Assertions are statements used to prove that a certain condition is true or has stayed true. If the condition proves to be false then the program will generally raise an AssertionError.

When to use Assertions

As powerful as assertions can be, they shouldn't be used haphazardly in your code as this could result in quite a mess especially when they are in a production environment, Ill explain this later. For now, the key point is that assertions are mainly used as a debugging aid to test conditions.

Assertion Syntax

The Python assert statement mainly consists of the assert keyword, the condition to test for and an optional assertion message to be returned if the condition proves to be false. This syntax is shown below:

odds = 4
assert odds < 3, 'odds less than 3 is required'

Enter fullscreen mode Exit fullscreen mode

If the condition evaluates to be true, then the program continues its execution as usual, otherwise, if it evaluates to be false, then it returns an AssertionError. The assert_message parameter though optional, is quite handy in providing a good description of the problem.

Common Assertion Types

Lets look at some of the commonly used assertion types in this section. There are various assertion types but for this article, well focus on three of the most common types:

  • Integrity/Identity Assertion: This type of assertion is used to test for an entity's identity thus proving its integrity. It checks for the data type of the entity in the assertion
a = 1
  b = 2
  assert a is a
Enter fullscreen mode Exit fullscreen mode
  • Association Assertion: This type of assertion is used to check if an entity is associated in any way with a given collection such as a list, set, tuple etc. It checks if the item is a member of that collection
def checker(fruit, fruits):
      assert fruit in fruits
      return True

  checker('apple', ['apple', 'banana', 'cherry'])
Enter fullscreen mode Exit fullscreen mode
  • Comparison: This type of assertion is used to compare two or more values
    assert 5 > 3

Enter fullscreen mode Exit fullscreen mode

Limitations of Assertions

As I mentioned earlier, assertions are quite powerful but when used randomly can lead one into a pitfall. To avoid this, it is quite necessary to know the limitations of assertions and under what conditions not to use them.

  • Data Validation: Assertions shouldn't be used to validate data or user input because assertions can be disabled globally thus resulting in unexpected results as well see in the code below:

  • Exception Handling: Assertions shouldn't be used as substitutes to proper exception handling in your code for the same reasons they shouldn't be used for data validation because they can be disabled.

Summarily, assertions are powerful debugging tools or aids. However, their usage should be done meticulously to avoid falling into one of its pitfalls

That's it for this article, Thanks for reading and don't break production with your assertion.

Top comments (0)