Documenting sorted/min/max prerequisites
The list.sort method is documented to only use less than: https://docs.python.org/3/library/stdtypes.html#list.sort but I don't think that is correct, it seems to use greater than if it exists and less than doesn't. My understanding is that items need to define one of l.t. or g.t. to make it sortable, not the full complement of six rich comparison methods. The same seems to be the case for sorted, min and max, but none of them have documented that fact: https://docs.python.org/3/library/functions.html#max https://docs.python.org/3/library/functions.html#min https://docs.python.org/3/library/functions.html#sorted Should we document that all four functions will use l.t. if it exists, otherwise g.t. if it exists, but don't need both? -- Steven
14.12.19 12:45, Steven D'Aprano пише:
The list.sort method is documented to only use less than:
https://docs.python.org/3/library/stdtypes.html#list.sort
but I don't think that is correct, it seems to use greater than if it exists and less than doesn't. My understanding is that items need to define one of l.t. or g.t. to make it sortable, not the full complement of six rich comparison methods.
What evidence do you have that it is not correct?
On Sat, Dec 14, 2019 at 02:40:04PM +0200, Serhiy Storchaka wrote:
14.12.19 12:45, Steven D'Aprano пише:
The list.sort method is documented to only use less than:
https://docs.python.org/3/library/stdtypes.html#list.sort
but I don't think that is correct, it seems to use greater than if it exists and less than doesn't. My understanding is that items need to define one of l.t. or g.t. to make it sortable, not the full complement of six rich comparison methods.
What evidence do you have that it is not correct?
I might be misinterpreting the evidence, but sorting works on objects that define `__gt__` without `__lt__`. py> class A: ... def __init__(self, x): self.x = x ... def __gt__(self, other): return self.x > other.x ... py> L = [A(9), A(1), A(8)] py> L.sort() py> [obj.x for obj in L] [1, 8, 9] But the main part of my question is whether we should make the same guarantee for sorted, min and max as we already make for list.sort. -- Steven
14.12.19 15:29, Steven D'Aprano пише:
I might be misinterpreting the evidence, but sorting works on objects that define `__gt__` without `__lt__`.
py> class A: ... def __init__(self, x): self.x = x ... def __gt__(self, other): return self.x > other.x ... py> L = [A(9), A(1), A(8)] py> L.sort() py> [obj.x for obj in L] [1, 8, 9]
This class supports '<':
A(1) < A(2) True
The `<` operator try to use `__lt__`, but if it is not defined falls back to `__gt__`.
On 14/12/2019 15.20, Serhiy Storchaka wrote:
14.12.19 15:29, Steven D'Aprano пише:
I might be misinterpreting the evidence, but sorting works on objects that define `__gt__` without `__lt__`.
py> class A: ... def __init__(self, x): self.x = x ... def __gt__(self, other): return self.x > other.x ... py> L = [A(9), A(1), A(8)] py> L.sort() py> [obj.x for obj in L] [1, 8, 9]
This class supports '<':
A(1) < A(2) True
The `<` operator try to use `__lt__`, but if it is not defined falls back to `__gt__`.
The operation "a < b" also fallback back to B.__gt__, when A.__lt__ returns NotImplemented for instances of B. Christian
On Sat, Dec 14, 2019 at 04:20:43PM +0200, Serhiy Storchaka wrote:
14.12.19 15:29, Steven D'Aprano пише:
I might be misinterpreting the evidence, but sorting works on objects that define `__gt__` without `__lt__`. [...] The `<` operator try to use `__lt__`, but if it is not defined falls back to `__gt__`.
Thanks Serhiy, that makes sense. The docs for special methods say that `__gt__` is the reflected version of `__lt__`. Which brings me back to my original question: Since list.sort guarantees that it will only use the `<` less than operator, should we make the same guarantee for sorted, min and/or max? It would be odd if the guarantee applies to list.sort but not sorted. -- Steven
15.12.19 12:27, Steven D'Aprano пише:
Since list.sort guarantees that it will only use the `<` less than operator, should we make the same guarantee for sorted, min and/or max?
Perhaps. And also for heapq and maybe some other functions. Do you mind to create a PR? It is common to make generic algorithms only depending on '<' and '==' (all standard C++ algorithms do this). But in any case it is better to implement all relation operators if you implement one. I do not know a case when you implement only '<'.
participants (3)
-
Christian Heimes
-
Serhiy Storchaka
-
Steven D'Aprano