Mark Dickinson <dicki...@gmail.com> added the comment:

To be precise, when doing `a < b`, either `a.__lt__` or `b.__gt__` can be used, 
since `__gt__` is considered the reversed / reflected version of `__lt__` 
(analogous to `__add__` and `__radd__`).


>>> class A:
...     def __lt__(self, other): return False
... 
>>> class B:
...     def __gt__(self, other): return True
... 
>>> A() < B()
False
>>> B() < A()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'B' and 'A'
>>> sorted([A(), B()])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'B' and 'A'
>>> sorted([B(), A()])
[<__main__.B object at 0x10dc4cca0>, <__main__.A object at 0x10dc68ca0>]

Presumably in the normal case, all the objects being sorted have the same type, 
and so in that case it's enough that the type implements at least one of __lt__ 
and __gt__.

----------
nosy: +mark.dickinson

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39210>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to