Do You Really Know How to Use Assertions Correctly?
What is an Assertion Assertions were invented as a debugging tool to check conditions that “should always be true if the code is correct.” For example, if we want to assert that a variable a must be greater than 2, we can write: assert a > 2 When the condition is not met, an AssertionError exception is raised, equivalent to the following code: if not assert_condition: raise AssertionError Since assertions are a debugging tool, Python’s implementation aligns with this philosophy. In Python, the execution of the assert statement depends on the __debug__ variable. The assert statement will only be executed if __debug__ is true. ...