First page Back Continue Last page Overview Graphics

Using assert

>>> x = 5

>>> y = 3

>>> assert x < y, "x has to be smaller than y"

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AssertionError: x has to be smaller than y

>>>

assert should not be used to catch programming errors like

x / 0, because Python traps such programming errors itself!

assert is for trapping user-defined constraints!