Python
assert (파이썬 키워드)
by yororing
2024. 7. 26.
00 개요
- 목적: 회사에서 코드분석 도중 assert라는 키워드가 나와서 이에 대해 정리하고자 함
01 assert 키워드
1. 정의 및 기능
- 코드 debugging 시 사용되는 키워드
- 주어진 조건이 True이면 아무것도 반환하지 않고 False이면 AssertionError를 raise하여 스크립트 실행을 종료함
2. 문법
assert 조건문[, 에러메세지]
# 예 - 에러메세지 없이 'assert 조건문'만 줬을 경우
>>> a = 'hello'
>>> assert a = 'hello' # 아무것도 반환하지 않음
>>> assert a == 'hell'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
# 예 - 에러메세지 포함
>>> a = 'banana'
>>> assert a == 'ban', 'ERROR!! ERRRRROOOOORRRR!!!!!!!!'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: ERROR!! ERRRRROOOOORRRR!!!!!!!!