자연스럽지 않은 None 사용
- 파이썬 프로그래머들은 유틸리티 함수를 작성할 때 반환 값
None
에 특별한 의미를 부여하는 경향이 있음
- 하지만 다음과 같은 경우에는 문제가 발생할 수 있음: 어떤 숫자를 다른 숫자로 나누는 헬퍼 함수 예제
- 이 경우 0으로 나누는 경우에는 결과가 정의되어 있지 않기 때문에
None
을 반환하는 것이 자연스러워 보임
- 하지만 분자가 0이되면 문제가 발생
- 반환 값도 0이 되어버리기 때문
if
문과 같은 조건에서 결과를 평가할 때 문제가 될 수 있음
- 아래 예제처럼 오류인지 확인하기 위해
None
대신 실수로 False
에 해당하는 값을 검사할 수도 있음
- 이러한 점들이 함수에서
None
을 반환하면 오류가 일어나기 쉬운 이유
None으로 인한 오류를 줄이는 방법
- 반환 값을 두 개로 나눠서 튜플에 담는 방법
- 튜플의 첫 번째 부분은 작업의 성공 유무를 알려줌
- 튜플의 두 번째 부분은 계산된 실제 결과
- 하지만 여전히 잘못된 사용을 할 가능성이 존재
Comments