How is True < 2 implemented?
True
is equal to 1 in Python (which is why it's less than 2) and bool
is a subclass of int
: basically, False
and True
are 0 and 1 with funky repr()
s.
As to how comparison is implemented on integers, Python uses __cmp__()
, which is the old-school way of writing comparisons in Python. (Python 3 doesn't support __cmp__()
, which is why it's implemented as __lt__()
there.) See https://docs.python.org/2/reference/datamodel.html#object.__cmp__
You didn't find super(bool, True).__lt__
because int
uses the legacy __cmp__
method instead of rich comparisons on Python 2. It's int.__cmp__
.
True
is a just name that refers to an object of type int
, specifically value 1. Expression True < 2
is equal to 1 < 2
. Same, False
is equal to 0. In Python 2 you have a method __cmp__
, that returns 0 if values are equals, -1 if is one value too greater than other value and 1 if is one value too less than other value. Example:
>>> True.__cmp__(1)0>>> True.__cmp__(0)1>>> True.__cmp__(-1)1>>> True.__cmp__(0)1>>> True.__cmp__(1)0>>> True.__cmp__(2)-1
In Python 3 you have a __lt__
and __gt__
methods that are equivalents of <
and >
.