Python’s Assert Statement: A Not-So Comprehensive Guide
Published: 2023-09-03 02:15:31

---
title: Python’s Assert Statement: A Not-So Comprehensive Guide
published: true
date: 2023-09-03 02:05:34 UTC
tags: 
canonical_url: https://psycode.hashnode.dev/pythons-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:

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

```

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
```python
a = 1
  b = 2
  assert a is a
```

- **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


```python
def checker(fruit, fruits):
      assert fruit in fruits
      return True

  checker('apple', ['apple', 'banana', 'cherry'])
```

- **Comparison:** This type of assertion is used to compare two or more values
```python
    assert 5 > 3

```

# **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.