[issue39210] Sorting falls back to use __gt__ when __lt__ is not present

2020-01-04 Thread Yan Mitrofanov


New submission from Yan Mitrofanov :

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@python
components: Documentation
messages: 359293
nosy: docs@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 
<https://bugs.python.org/issue39210>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39210] Sorting falls back to use __gt__ when __lt__ is not present

2020-01-04 Thread Yan Mitrofanov


Yan Mitrofanov  added the comment:

I got your point. So it seems that two pieces of documentation are not 
equivalent: 

https://docs.python.org/3/howto/sorting.html#odd-and-ends
> The sort routines are guaranteed to use __lt__() when making comparisons 
> between two objects.

https://docs.python.org/3/library/stdtypes.html#list.sort
> This method sorts the list in place, using only < comparisons between items.

--

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