+ 2
What are the differences between assertion and if?
3 odpowiedzi
+ 6
An if statement will not raise an exception when the condition is false.
Assertion will raise an assertion error when the condition is false. An assertion exception will stop the program unless the exception is handled by a try-except block.
+ 4
look at assertions as a method for development. so you can build various checkpoints across the code to ensure that variables/arguments are meeting the expected limitations (e.g. you know it must be number not letter, it must be positive , etc...).
Then once you're done with development you can globally turnoff all the assertions.
In case you will use IF statements (instead of assertions) for this, you will need to clean up the code and possibly introduce some errors.
0
both are similar only difference is that unlike if statement, assertion raise an assertion error if condition is false.