[Python-checkins] sorting howto: Add clarification on < using __lt__ (GH-92010)

miss-islington webhook-mailer at python.org
Fri Apr 29 18:26:56 EDT 2022


https://github.com/python/cpython/commit/869a89433899950f925d177bacd8fdd43affe827
commit: 869a89433899950f925d177bacd8fdd43affe827
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-04-29T15:26:51-07:00
summary:

sorting howto: Add clarification on < using __lt__ (GH-92010)

(cherry picked from commit 53ca774497fde7c5fcf3a84813ea42f95f75c639)

Co-authored-by: slateny <46876382+slateny at users.noreply.github.com>

files:
M Doc/howto/sorting.rst

diff --git a/Doc/howto/sorting.rst b/Doc/howto/sorting.rst
index a8efe65353d6e..be017cfba35ff 100644
--- a/Doc/howto/sorting.rst
+++ b/Doc/howto/sorting.rst
@@ -287,7 +287,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::
 
@@ -295,6 +295,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



More information about the Python-checkins mailing list