[New-bugs-announce] [issue39210] Sorting falls back to use __gt__ when __lt__ is not present

Yan Mitrofanov report at bugs.python.org
Sat Jan 4 10:29:55 EST 2020


New submission from Yan Mitrofanov <mmxlviii at mail.ru>:

Sorting documentation claims that sorting algorithm is only using < comparisons
https://docs.python.org/3/howto/sorting.html#odd-and-ends
https://docs.python.org/3/library/stdtypes.html#list.sort

When __lt__ implementation is missing, you get an exception
class Foo:
    pass
sorted([Foo(), Foo(), Foo()])
TypeError: '<' not supported between instances of 'Foo' and 'Foo'

However, if implement __gt__ method, you doesn't get an exception
class Foo:
    def __gt__(self, other):
        return False
sorted([Foo(), Foo(), Foo()])  # ok


Is it supposed to work like this? Or is it lack of documentation?

----------
assignee: docs at python
components: Documentation
messages: 359293
nosy: docs at python, yanmitrofanov
priority: normal
severity: normal
status: open
title: Sorting falls back to use __gt__ when __lt__ is not present
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39210>
_______________________________________


More information about the New-bugs-announce mailing list