]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
sorting howto: Add clarification on < using __lt__ (#92010)
authorslateny <46876382+slateny@users.noreply.github.com>
Fri, 29 Apr 2022 22:08:07 +0000 (15:08 -0700)
committerGitHub <noreply@github.com>
Fri, 29 Apr 2022 22:08:07 +0000 (16:08 -0600)
Doc/howto/sorting.rst

index 37328c82dff270f317b05e7ba21bea41b97f436c..32b47711f85705a34d42fd7c2dd054e25ed7feb7 100644 (file)
@@ -325,7 +325,7 @@ Odd and Ends
     >>> standard_way
     [('red', 1), ('red', 2), ('blue', 1), ('blue', 2)]
 
-* The sort routines are guaranteed to use :meth:`__lt__` when making comparisons
+* The sort routines use ``<`` when making comparisons
   between two objects. So, it is easy to add a standard sort order to a class by
   defining an :meth:`__lt__` method:
 
@@ -335,6 +335,9 @@ Odd and Ends
     >>> sorted(student_objects)
     [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
 
+  However, note that ``<`` can fall back to using :meth:`__gt__` if
+  :meth:`__lt__` is not implemented (see :func:`object.__lt__`).
+
 * Key functions need not depend directly on the objects being sorted. A key
   function can also access external resources. For instance, if the student grades
   are stored in a dictionary, they can be used to sort a separate list of student