+ 3
What's the use of assertion in Python
3 Réponses
+ 4
Possibly because in the event of a false condition it will terminate a program with an AssertionError but honestly I don't use it that frequently.
0
Assert can be used to debug or prevent wrong input from user.
Like assert (age<0), 'incorrect age'
Also, can be used for debugging and knowing exactly where the values are wrongly compared. Like if you are in a while loop, and there are multiple layers of while and if else statements, and code goes wrong, you can put assert as checks and find out exactly when the code is going wrong.
0
For example, we have a statement that must be provided when writing a program. In such cases, we can quickly find errors when there is an error by using the assert method. Thanks to this method in long and complex programs, we can immediately catch the error and correct it. Otherwise, we have to examine all our code thoroughly to debug it. We can easily disable it at any time. Let's say we used the if method; And we don't want the program to run faster and take up too much space. Then we will have to delete all our entries one by one, and while deleting them, there is a danger of accidentally deleting if else codes that we use for other purposes. That's why the assert method makes our job much easier in debugging.