The manual says:
in general, __lt__()and __eq__()are sufficient if you want conditional values ββof comparison operators
But I see an error:
> assert 2 < three
E TypeError: unorderable types: int() < IntVar()
when i run this test:
from unittest import TestCase
class IntVar(object):
def __init__(self, value=None):
if value is not None: value = int(value)
self.value = value
def __int__(self):
return self.value
def __lt__(self, other):
return self.value < other
def __eq__(self, other):
return self.value == other
def __hash__(self):
return hash(self.value)
class DynamicTest(TestCase):
def test_lt(self):
three = IntVar(3)
assert three < 4
assert 2 < three
assert 3 == three
I am surprised that when IntVar()on the right, __int__()it is not called. What am I doing wrong?
Adding __gt__()fixes this, but means I donβt understand what the minimum requirements for ordering ...
Thanks Andrew
source
share