Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3
Assertions can be thought of as validators, used to make sure (assert) that the evaluation of an expression meets a condition. Or to put it simply, make sure that a variable meets a given condition. for example: assert x == 2 is the same as doing: if not (x == 2): raise AssertionError() The intended application of assertions is to debug your code without having to use external debugging tools. Just stick an assertion into your code to check a variable for some condition and if the assertion isn't met, you know where your code is falling over. In this code I use assertions to implement static type checking in python: https://code.sololearn.com/cI1r0xgbwDbF/?ref=app In any case, avoid using assertions in production code for things like validating user input because assertions can be turned off entirely in the interpreter, leaving them to be ignored.
22nd Aug 2018, 9:05 PM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar