python assert断言的作用
python assert断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假。可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会触发异常。
In [1]: lists = [1,2]
In [2]: assert len(lists) >= 5, '列表元素小于5'
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-2-4c92d72b075d> in <module>()
----> 1 assert len(lists) >= 5, '列表元素小于5'
AssertionError: 列表元素小于5
In [3]: assert 2==1, '2不等于1'
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-3-a1cc3d9659f9> in <module>()
----> 1 assert 2==1, '2不等于1'
AssertionError: 2不等于1