Python3中assert断言

一般的用法是:

assert condition

用来让程序测试这个condition,如果condition为false,那么raise一个AssertionError。逻辑上等于:

if not condition:
    raise AssertionError()

比如如下的例子:

>>> assert 1==1
>>> assert 1==0
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    assert 1==0
AssertionError
>>> assert True
>>> assert False
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    assert False
AssertionError
>>> assert 3<2
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    assert 3<2
AssertionError

 

posted on 2018-08-09 15:22  异彩之光  阅读(179)  评论(0编辑  收藏  举报

导航