Assertion error in python
In this article, we will discuss assertions in the python programming language.
Assertion:
Assertions are a way of telling a program to test a certain condition and trigger an error if the condition is false.
The basic syntax of assertions in python involves a keyword ' assert ', an assertion condition, and an optional message.
SYNTAX:
assert < condition >, < optional message (String) >
Example:
# To take the age of a person as input and create an assertion error if the provided age is less than zero.
def age( a ):
assert a > 0, " Age cannot be less than zero. "
print( f" So your age is { a } " )
age( 20 )
OUTPUT:
So your age is 20
Process finished with exit code 0
# To take the age of a person as input and create an assertion error if the provided age is less than zero.
def age( a ):
assert a > 0, " Age cannot be less than zero. "
print( f" So your age is { a } " )
age( -20 )
OUTPUT:
Traceback (most recent call last):
File "xxx\main.py", line 5, in <module>
age( -20 )
File "xxx\main.py", line 3, in age
assert a > 0, " Age cannot be less than zero. "
AssertionError: Age cannot be less than zero.
Process finished with exit code 1
This is an easier way of debugging your code, in which case it is easier to track down the errors and fix the bugs in your program.
Assertion Error:
If the programmer writes a block of code which is used to declare a condition to be true before the module is run, then such type of programming concept is called Assertion Error.
This is similar to any kind of error that occurs in python, i.e., if the condition provided while declaring the assertion is true, then the control is simply moved to the next line in the code. Whereas when it is false, then an error is raised, and the program is terminated.
The assert statement is independent of language, i.e., it can be used in other programming languages as well with their respective syntaxes.
AssertionError is a subclass of the Exception class.
Assertion Error exception handling:
Assertion error is handled in two ways:
- by the default exception handler
- by user
Example:
# program where Assertion error is handled by the default exception handler
def age( a ):
assert a > 0 , " Age cannot be less than zero "
print( f" So your age is { a } " )
age( - 1 )
OUTPUT:
Traceback (most recent call last):
File "xxx\main.py", line 5, in <module>
age( - 1 )
File "xxx\main.py", line 3, in age
assert a > 0, " Age cannot be less than zero "
AssertionError: Age cannot be less than zero
Process finished with exit code 1
Example:
# program where Assertion error is handled by the user
def age( a ):
assert a > 0 , " Age cannot be less than zero "
print( f" So your age is { a } " )
try :
age( - 1 )
except AssertionError as e :
print( e )
OUTPUT:
Age cannot be less than zero
Process finished with exit code 0
Practical applications of Assertion error involve testing a program, debugging, solving smaller blocks of code etc.
Applications of Assertion Error:
The other useful applications of assertion error involve
- Assertion helps in checking the values of parameters
- Assertions help in keeping an eye on the input type (whether valid or not).
- Assertions also help to detect if an interface is abused by another code developer
- Assertions help to keep a constant check on the output of the program.
To avoid assert errors in python:
- In a python program, to disable all the assertion statements present in the program, we can use the -O flag. We need to note that when the assertion statements are disabled, the statements that follow the assertion statement are not executed.
- We can also set a flag by making use of the environment variable to disable the assertion statements. In such a case, all the processes and programs that inherit and use the environment are affected.
- The simplest way to avoid assertion errors in python is to handle them manually, i.e., if we make sure that the flow of the control of the program is diverted in such a way that it does not reach the assert statements, then, there is no possibility for assertion errors ( exception handling – try, catch )