본문 바로가기

컴퓨터/Python

assert 구문

인자로 받은 조건식이 거짓인 경우 AssertError가 발생

assert <조건식>, <관련 데이터>


def foo(x):

assert type(x) == int, "Input value must be integer"

return x * 10


ret = foo("a")

print(ret)


Traceback (most recent call last):

  File "/Users/MGP/Documents/workspace/Python/src/testmodule/__init__.py", line 5, in <module>

    ret = foo("a")

  File "/Users/MGP/Documents/workspace/Python/src/testmodule/__init__.py", line 2, in foo

    assert type(x) == int, "Input value must be integer"

AssertionError: Input value must be integer


'컴퓨터 > Python' 카테고리의 다른 글

출력  (0) 2013.07.25
repr(), str(), ascii()  (0) 2013.07.25
raise 구문  (0) 2013.07.24
사용자정의 예외  (0) 2013.07.24
예외처리  (0) 2013.07.23