assert 斷言

assert

通常用法是:shell

assert condition

用來讓程序測試這個condition,若是condition爲False則拋出一個AssertionError,邏輯上等同於測試

if not condition:
    raise AssertionError()

好比下面的例子code

>>> 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
相關文章
相關標籤/搜索