Message359295
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__. |
|
Date |
User |
Action |
Args |
2020-01-04 16:19:52 | mark.dickinson | set | recipients:
+ mark.dickinson, steven.daprano, docs@python, yanmitrofanov |
2020-01-04 16:19:52 | mark.dickinson | set | messageid: <1578154792.68.0.680754568398.issue39210@roundup.psfhosted.org> |
2020-01-04 16:19:52 | mark.dickinson | link | issue39210 messages |
2020-01-04 16:19:52 | mark.dickinson | create | |
|