+ 1

Assertion

Can somebody explain to me what an assertion is, I haver done it on the Sololearn, but U could not understand, then I have watched different videos on youtube, but still I could not understand, can somebody please explain me what an assertion is? Thank you

12th May 2022, 9:20 AM
Danish Zubair
Danish Zubair - avatar
9 Answers
+ 5
An assertion, is a condition about a particular value, that you want your program to enforce, otherwise the value does not make sense. When an assertion fails, the program will stop with AssertionError. This is typically used to write tests for your program. You can think of it as a validation, that the data you receive, has the required shape or form or is within the expected range of values. For example, you want to evaluate if a score on a math test passes or not. The score can be between 0 and 100. So if you receive -9999 as test score, your program can fail with assertion error, because the input is obviously wrong. You can read more about the topic here: https://dbader.org/blog/JUMP_LINK__&&__python__&&__JUMP_LINK-assert-tutorial
12th May 2022, 9:46 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Danish Zubair yes you are correct.
12th May 2022, 10:42 AM
Tibor Santa
Tibor Santa - avatar
+ 2
I would not use an assertion to validate data that a program receives. An assertion, to me, is something that is always true. If not, the program is broken. To pick up the idea of a multimeter used by Rik Wittkopp, an assertion would be in the circuit diagram that at a certain point there are five volts. If there aren't the circuit is broken. You can now take your multimeter and measure, and you better read five volts, or you may have to replace parts or adjust a varicap. User input is always tainted. That it lies outside valid values is to be expected. But if you write a library as part of a larger application, and it receives only untainted data, and by design some variable is guaranteed never to be null, then you can assert non-null on that variable. If one day it is null, it is a serious bug somewhere. The assertion failure will show. A "if(var is not null)" will not. For invalid user data, a strategy to handle it gracefully is better than to crash the program with an AssertionError in my opinion.
14th May 2022, 6:28 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 1
Danish Zubair An assertion is a bit of code that has been written to prove the validity of a piece of the code you wish to present to the world. If your code was a complex electrical circuit, the the assertion would be the multimeter used to validate each section of the code. Assertions, like multimeters are useless if not employed correctly. The trick is to devise a test that will prove/disprove an expected result
12th May 2022, 9:42 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Rik Wittkopp thank you
12th May 2022, 10:06 AM
Danish Zubair
Danish Zubair - avatar
+ 1
Tibor Santa, so assertion check if the condition is true, then it will show no Assertion errors and it will run the code after it, and if the condition is false, it will show an assertion error which can be modified. Let me know if I am correct. Thank you soo much for your explanation.
12th May 2022, 10:11 AM
Danish Zubair
Danish Zubair - avatar
+ 1
Tibor Santa Thank you sooo much
12th May 2022, 11:44 AM
Danish Zubair
Danish Zubair - avatar
+ 1
Ani Jona šŸ•Š thank you for your explanation
14th May 2022, 8:08 AM
Danish Zubair
Danish Zubair - avatar
0
Is my explanation of Assertion correct?
12th May 2022, 10:19 AM
Danish Zubair
Danish Zubair - avatar