Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4531f8d

Browse files
committed
Issue #9886: Add a key-function entry to the glossary.
1 parent c6b6b5b commit 4531f8d

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Doc/glossary.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,26 @@ Glossary
283283
instances of user-defined classes are hashable by default; they all
284284
compare unequal, and their hash value is their :func:`id`.
285285

286+
key function
287+
A key function or collation function is a callable that returns a value
288+
used for sorting or ordering. For example, :func:`locale.strxfrm` is
289+
used to produce a sort key that is aware of locale specific sort
290+
conventions.
291+
292+
A number of tools in Python accept key functions to control how elements
293+
are ordered or grouped. They include :func:`min`, :func:`max`,
294+
:func:`sorted`, :meth:`list.sort`, :func:`heapq.nsmallest`,
295+
:func:`heapq.nlargest`, and :func:`itertools.groupby`.
296+
297+
There are several ways to create a key function. For example. the
298+
:meth:`str.lower` method can serve as a key function for case insensitive
299+
sorts. Alternatively, an ad-hoc key function can be built from a
300+
:keyword:`lambda` expression such as ``lambda r: (r[0], r[2])``. Also,
301+
the :mod:`operator` module provides three key function constuctors:
302+
:func:`~operator.attrgetter`, :func:`~operator.itemgetter`, and
303+
:func:`~operator.methodcaller`. See the :ref:`Sorting HOW TO` for
304+
examples of how to create and use key functions.
305+
286306
IDLE
287307
An Integrated Development Environment for Python. IDLE is a basic editor
288308
and interpreter environment which ships with the standard distribution of

0 commit comments

Comments
 (0)