3 ms·
The document explains it. When dealing with these comparators, when chained they get separated and you have the equivalent of this: False == False in [True]
by Jtsummers 1y ago
The document explains it. When dealing with these comparators, when chained they get separated and you have the equivalent of this:
False == False in [True]
=>
(False == False) and (False in [True])
If you put it into a function like this:
def foo(): return False == False in [True]
And then disassemble it you'll get this byte code (3.13, may vary by version):
RESUME 0
LOAD_CONST 1 (False)
LOAD_CONST 1 (False)
SWAP 2
COPY 2
COMPARE_OP 72 (==)
COPY 1
TO_BOOL
POP_JUMP_IF_FALSE 5 (to L1)
POP_TOP
LOAD_CONST 2 ((True,))
CONTAINS_OP 0
RETURN_VALUE
L1: SWAP 2
POP_TOP
RETURN_VALUE
There is no literal `and` operation occurring here, instead it first does the `False == False`, then jumps if false or continues to the second comparison.
For the literals we've given it, it'll go on to the second comparison which will result in false since `False` is not in `(True,)`.
EDIT: And here's the non-chained version disassembled:
def bar(): return (False == False) in [True]
RESUME 0
LOAD_CONST 1 (False)
LOAD_CONST 1 (False)
COMPARE_OP 72 (==)
LOAD_CONST 2 ((False,))
CONTAINS_OP 0
RETURN_VALUE
- jnpnj 1y agoThanks a lot, brilliant