assert statement
Programmers use assert statements to verify expectations, such as the output of a function being tested.
boolean context。シンプルにfalse
がでたらエラーを吐いて""
を表示してくれるstatement
。true
の場合は特に何も処理を行わない。
一般化するとassert (conditional statement), "otherwise reason or exception says"
こういったテストは同じファイルの中で書くか、_test.py
と名付け別のファイルとして保存するのが通例。
your_age = int(input("your age is?"))
assert your_age >= 0, "how is your age negative?" # age always has to be greater than equal 0, otherwise...
print("your age is:", your_age)
ただfalse
が出るとプログラム自体がクラッシュしてしまうのであまり好まれない。
ref: