What are the big differences between raise AssertionError
and assert
to build in a "fault"? What are the effects on the code? And is one or the other more pythonic in a way?
The reason for my question is because I am learning to program. Right now we have an exercise where for example when x != 0
we need to get an AssertionError
"false".
I looked this up online, where I found the following code:
if x != 0:
raise AssertionError ("false")
But my teachers also use the following a lot:
assert x == 0,"fout"
What are the (dis)advantages of each approach?
Thanks a lot in advance.
assert
statements can be ignored with compiler flags.assert
statement is generally for unit-testing or finding issues during the development process. This does not mean you will necessarily see the error. On the other hand, raising anAssertionError
means that the error will be raised by the code, and that any calling processes can deal with this error as needed.