Consider
1 in [] == False
On a very quick glance, it appears that this will raise a TypeError: bool is not iterable
, but it actually executes (and evaluates to False
if you are curious, which is also a bit surprising).
However,
1 in [1] == False
is also evaluating to False
.
It is interesting to note that 1 in [] == False
does not:
- Evaluate as
1 in ([] == False)
- This causes the expectedTypeError
mentioned above. - Evaluate as
(1 in []) == False
- This can be confirmed by(1 in []) == False
which outputsTrue
, compared to1 in [] == False
which evaluates toFalse
.
The only output that does make sense is (1 in [1]) == False
which is False
.
dis.dis
outputs:
dis.dis('1 in [] == False')
print()
print()
print()
dis.dis('(1 in [1]) == False')
print()
print()
print()
dis.dis('1 in ([] == False)')
outputs
# 1 in [] == False
1 0 LOAD_CONST 0 (1)
2 BUILD_LIST 0
4 DUP_TOP
6 ROT_THREE
8 COMPARE_OP 6 (in)
10 JUMP_IF_FALSE_OR_POP 18
12 LOAD_CONST 1 (False)
14 COMPARE_OP 2 (==)
16 RETURN_VALUE
>> 18 ROT_TWO
20 POP_TOP
22 RETURN_VALUE
# (1 in [1]) == False
1 0 LOAD_CONST 0 (1)
2 LOAD_CONST 1 ((1,))
4 COMPARE_OP 6 (in)
6 LOAD_CONST 2 (False)
8 COMPARE_OP 2 (==)
10 RETURN_VALUE
# 1 in ([] == False)
1 0 LOAD_CONST 0 (1)
2 BUILD_LIST 0
4 LOAD_CONST 1 (False)
6 COMPARE_OP 2 (==)
8 COMPARE_OP 6 (in)
10 RETURN_VALUE
I'm especially intrigued by the ROT_THREE
, ROT_TWO
and JUMP_IF_FALSE_OR_POP
instructions.