TLDR: Python Internals - Exploring Chained Comparison Operators
Date: 2021-04-28 Source: https://arpitbhayani.me/blogs/chained-operators-python
Overview
Explore Python's chained comparisons (like a < b < c) and short-circuiting. Learn how they work under the hood and how to modify CPython! 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.
Key Points
- how Python evaluates chained comparison operators?
- how Python implements short-circuiting?
- how could you make Python-like evaluation a C-like evaluation? implying at the end of this essay we alter the CPython source code such that the expression -3 < -2 < -1
- Evaluating
1 < 2 < 3: When we run disassembler on 1 < 2 < 3 we get a similar disassembled code.