diff options
author | slateny <46876382+slateny@users.noreply.github.com> | 2022-04-29 22:08:07 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-29 22:08:07 (GMT) |
commit | 53ca774497fde7c5fcf3a84813ea42f95f75c639 (patch) | |
tree | 0a5f5057cf51d0b33205e94ccb48cd1766bdd16f /Doc | |
parent | 81120b6754c67a8f1f00cbc3af6963c0e1945911 (diff) | |
download | cpython-53ca774497fde7c5fcf3a84813ea42f95f75c639.zip cpython-53ca774497fde7c5fcf3a84813ea42f95f75c639.tar.gz cpython-53ca774497fde7c5fcf3a84813ea42f95f75c639.tar.bz2 |
sorting howto: Add clarification on < using __lt__ (#92010)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/howto/sorting.rst | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/Doc/howto/sorting.rst b/Doc/howto/sorting.rst index 37328c8..32b4771 100644 --- a/Doc/howto/sorting.rst +++ b/Doc/howto/sorting.rst @@ -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 |