Chained Comparison Operators in Python
Chained Comparison Operators in Python
Chained Comparison Operators in Python
Python supports chaining of comparison operators, which means if we wanted to find out
if b lies between a and c we can do a < b < c, making code super-intuitive. Python
evaluates such expressions like how we do in mathematics. which means a < b < c is
evaluated as (a < b) and (b < c). C language on the other hand, evaluates a < b <
c as ((a < b) < c).
Depending on how we evaluate such expressions, the final evaluated changes. So, in python,
if we evaluate -3 < -2 < -1, we get True and if evaluate 3 > 2 == 1 we get False.
>>> -3 < -2 < -1
True
>>> 3 > 2 == 1
False
But on the other hand, if we evaluate this very expression in C language the output is False.
#include <stdio.h>
return 0;
$ gcc test.cpp
$ ./a.out
It does so because (-3 < -2) = True = 1 and 1 < -1 is False. Also, to get a better
understanding of how such expressions evaluate, try playing around with different values and
see if your predicted value matches the actual output.
This is possible because internally Python evaluates this chained expression a < b <
c as (a < b) and (b < c). To make this efficient, the sub-expression b is evaluated only
once and the evaluation also follows short-circuit evaluation; which means, if (a < b) is
evaluated as False then Python would not evaluate further sub-expressions - c and (b < c).
Now that we have set the context, let's find out what happens under the hood.
1 0 LOAD_NAME 0 (a)
2 LOAD_NAME 1 (b)
4 DUP_TOP
6 ROT_THREE
8 COMPARE_OP 0 (<)
10 JUMP_IF_FALSE_OR_POP 18
12 LOAD_NAME 2 (c)
14 COMPARE_OP 0 (<)
16 RETURN_VALUE
>> 18 ROT_TWO
20 POP_TOP
22 RETURN_VALUE
Here is the summary of what each of the above instructions does; having this understanding
will help us understand the entire execution process.
Now COMPARE_OP pops out two elements, compares them, and since 2 < 3 it evaluates
to True and this True is stacked on top. After this operation, the stack has just one
element True.
The next instruction is RETURN_VALUE, which pops out the top of the stack i.e. True, and
returns it; and this is how the expression 1 < 2 < 3 is evaluated to True.
Short-circuit Evaluation
A very interesting instruction is sitting right in the middle - JUMP_IF_FALSE_OR_POP. This
instruction is the one that is doing short-circuiting. Once the runtime encounters this
instruction it checks the top of the stack,
if top == False the flow jumps to the last few instructions, bypassing the
loading and comparing other sub-expressions.
if top == True it does not jump, but rather continues its evaluation of the next
instructions.
To get a better understanding, try doing an instruction by instruction walkthrough for the
expression 6 > 7 > 8 and you will find out how it bypasses the evaluating next sub-
expressions.
Now we know, why the official documentation says,
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <=
z, except that y is evaluated only once (but in both cases z is not evaluated at all when x
< y is found to be false).
So, if we keenly observe, to evaluate 1 < 2 < 3 as (1 < 2) and (2 < 3) we would need
to repeat the middle operand and keep it ready as the first operand of the second comparison.
Now, to "repeat" the middle operand, we call DUP_TOP.
Once the two operands are loaded on the stack we see that the right operand sits on the top
and by invoking DUP_TOP we are copying the middle operand and putting it on the top of the
stack. This copied top (middle operand) needs to be preserved to be used as the first operand
in the next comparison, and to do this we call ROT_THREE that puts the stack top to the third
from the top.
After the first comparison is evaluated the stack contains - the copied middle operand and on
top of it the evaluated value. The evaluated value is discarded or returned depending on if it
is True or False, keeping the copied middle operand on the stack, making it the first
operand of the next comparison.
...
VISIT(c, expr,
(expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
ADDOP(c, DUP_TOP);
ADDOP(c, ROT_THREE);
NEXT_BLOCK(c);
...
ADDOP(c, ROT_TWO);
ADDOP(c, POP_TOP);
...
return 1;
...
VISIT(c, expr,
(expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
// ADDOP(c, DUP_TOP);
// ADDOP(c, ROT_THREE);
NEXT_BLOCK(c);
...
ADDOP(c, ROT_TWO);
ADDOP(c, POP_TOP);
...
return 1;
Recall that the expression -3 < -2 < -1 on a usual Python interpreter evaluates
to True because -2 is between -3 and -1. But post these changes, if we build the binary and
start the interpreter we would see the output of expression -3 < -2 < -1 as False, just like
C; as it evaluated the expression from left to right and kept reusing the output of the previous
comparison as the first operand of the next one.
Here is the disassembled code and an instruction by instruction execution post our changes.
The engine first evaluated -3 < -2, and put the result True on top of the stack and then
loaded -1 to perform the comparison True < -1. Since True == 1 the expression True <
-1 is evaluated as False and hence the output of the entire statement is False, just like in C.
This also means that the expression 3 > 2 == 1 should evaluate to True and it actually
does.
References