如题所述
>>> a = True
>>> b = False
>>> assert a
>>> assert b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert a and b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert a or b
>>> b = False
>>> assert a
>>> assert b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert a and b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert a or b
看看上面就可以知道大概的作用了,可以用于测试吧 assert后面期待结果是布尔值True,表达式(a or b)返回的值也为True,所以就不会抛出异常。
温馨提示:答案为网友推荐,仅供参考
第1个回答 2015-09-30
断言,一般用于单元测试