[python]assert文


条件に基づいてエラーメッセージを出力する場合、statement.
以下の構成を使用します.(ただし、メッセージは省略できます.)
assert 조건문, message
デバッグモードでのみ動作する例外処理方法です.
たとえば、次のように使用できます.
def check(a):
	assert 3 <= a < 6, 'our of range'
この場合は下図のように記入すれば通過します.
check(4)
逆に、範囲外の(8)を入力するとエラーが発生します.
check(8)

Traceback (most recent call last):
  File "/opt/ml/code/github/baseline/testcode.py", line 3, in <module>
    check(7)
  File "/opt/ml/code/github/baseline/testcode.py", line 2, in check
    assert 3<=a <= 6, 'out of range'
AssertionError: out of range