왜 (1 in [1,0] == True) 는 False 일까요?
syntax
python
operation-precedence
발생하는 문제 및 실행환경
(1 in [1,0] == True)
가 어떻게 구문분석 되는질 모르겠어서 나름대로 시도해봤는데
>>> 1 in [1,0] # 원래 그런거니까..
True
>>> 1 in [1,0] == True # 이상함
False
>>> (1 in [1,0]) == True # 이렇게 구문 분석 될거라 생각했습니다
True
>>> 1 in ([1,0] == True) # 에러
4번째 코드에서 에러내용
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
1 in ([1,0] == True)
TypeError: argument of type 'bool' is not iterable
-
2016년 02월 16일에 작성됨
조회수 582
1 답변
파이썬은 이런 구문을 보면 보통 chained comparisons를 합니다.
(1 in [1,0] == True)
는 사실
(1 in [1, 0]) and ([1, 0] == True)
와 같기 때문에 False가 되는 거지요.
자세한 건
이미 이쪽 질문 [왜 파이썬 (0 < 0 == 0)는 False를 return하나요?]에 대답해놨습니다.
-
2016년 02월 16일에 작성됨
출처 : https://stackoverflow.com/questions/9284350/why-does-1-in-1-0-true-evaluate-to-false 이 질문은 저작자표시-동일조건변경허락(https://creativecommons.org/licenses/by-sa/3.0/deed.ko) 라이센스로 이용할 수 있습니다. 윤동길 2018.3.30 11:45