In python we can say:
if foo < bar < baz:
do something.
and similarly, we can overload the comparision operators like:
class Bar:
def __lt__(self, other):
do something else
but what methods of the types of the operands of those interval comparisions are actually called? is the above equivalent to
if foo.__lt__(bar) and bar.__lt__(baz):
do something.
Edit: re S.Lott, Here’s some output that helps to illustrate what actually happens.
>>> class Bar:
def __init__(self, name):
self.name = name
print('__init__', self.name)
def __lt__(self, other):
print('__lt__', self.name, other.name)
return self.name < other.name
>>> Bar('a') < Bar('b') < Bar('c')
('__init__', 'a')
('__init__', 'b')
('__lt__', 'a', 'b')
('__init__', 'c')
('__lt__', 'b', 'c')
True
>>> Bar('b') < Bar('a') < Bar('c')
('__init__', 'b')
('__init__', 'a')
('__lt__', 'b', 'a')
False
>>>
You are correct:
Output: