Если вы определяете __lt__ (<), то неявно автоопределится __gt__ (>), но не __ge__ (>=) и __le__ (<=).
>>> class A:
... def __lt__(self, v):
... return hasattr(v, 'x')
...
>>> class B:
... x = 1
...
>>> class C:
... y = 1
...
>>> a, b, c = A(), B(), C()
>>>
>>> a < b
True
>>> a > b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: A() > B()
>>> a == b
False
>>> a != b
True
>>> a <= b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: A() <= B()
>>> a >= b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: A() >= B()
>>>
>>> a < c
False
>>> a > c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: A() > C()
>>> a == c
False
>>> a != c
True
>>> a <= c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: A() <= C()
>>> a >= c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: A() >= C()
>>>
>>> class Test:
def __init__(self,value):
self._value = value
#
def __lt__(self,other):
if type(other) != Test:
raise ValueError('comparing object must be of type Test')
return self._value < other._value
... ... ... ... ... ... ... ...
>>> a = Test(10)
>>> b = Test(20)
>>> a < b
True
>>> a > b
False
>>> a < 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in __lt__
ValueError: comparing object must be of type Test >>> class Test:
... def __init__(self,value):
... self._value = value
...
... def __lt__(self,other):
... if type(other) != Test:
... raise ValueError('comparing object must be of type Test')
... return self._value < other._value
...
>>> a = Test(10)
>>> b = Test(20)
>>> a < b
True
>>> a > b
False
>>> a < 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in __lt__
ValueError: comparing object must be of type Test
>>> a > 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: Test() > int()
>>> >>> class Test:
... def __init__(self,value):
... self._value = value
...
... def __lt__(self,other):
... print('lt')
... if type(other) != Test:
... raise ValueError('comparing object must be of type Test')
... return self._value < other._value
...
>>> a = Test(10)
>>> b = Test(20)
>>> a < b
lt
True
>>> a > b
lt
False
>>>
>>> class Test:
... def __init__(self, value):
... self.value = value
... def __lt__(self, other):
... print('lt')
... print(self.value)
... return self.value < other.value
...
>>> a = Test(10)
>>> b = Test(20)
>>> a < b
lt
10
True
>>> a > b
lt
20
False
>>>