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 _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/PAMPK6TS7TVBDAB54OEJEBJCNT75VW3X/ Code of Conduct: http://python.org/psf/codeofconduct/