This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients docs@python, mark.dickinson, steven.daprano, yanmitrofanov
Date 2020-01-04.16:19:52
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1578154792.68.0.680754568398.issue39210@roundup.psfhosted.org>
In-reply-to
Content
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__.
History
Date User Action Args
2020-01-04 16:19:52mark.dickinsonsetrecipients: + mark.dickinson, steven.daprano, docs@python, yanmitrofanov
2020-01-04 16:19:52mark.dickinsonsetmessageid: <1578154792.68.0.680754568398.issue39210@roundup.psfhosted.org>
2020-01-04 16:19:52mark.dickinsonlinkissue39210 messages
2020-01-04 16:19:52mark.dickinsoncreate