assert 断言
assert
一般用法是:
assert condition
用来让程序测试这个condition,如果condition为False则抛出一个AssertionError,逻辑上等同于
if not condition:
raise AssertionError()
比如下面的例子
>>> assert 1==1
>>> assert 1==0
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
assert 1==0
AssertionError
>>> assert True
>>> assert False
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
assert False
AssertionError
>>> assert 3<2
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
assert 3<2
AssertionError