summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-12-01 13:18:39 (GMT)
committerRaymond Hettinger <python@rcn.com>2003-12-01 13:18:39 (GMT)
commit166958b5df50fca05cb24be0152737edf575dbb9 (patch)
tree2cc504f8bac567c6ef0c6b02b05d30b30b6b6923 /Doc
parenta45517065a01a98fb99e77daa74e7b5e85e889e8 (diff)
downloadcpython-166958b5df50fca05cb24be0152737edf575dbb9.zip
cpython-166958b5df50fca05cb24be0152737edf575dbb9.tar.gz
cpython-166958b5df50fca05cb24be0152737edf575dbb9.tar.bz2
As discussed on python-dev, added two extractor functions to the
operator module.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/lib/liboperator.tex33
1 files changed, 33 insertions, 0 deletions
diff --git a/Doc/lib/liboperator.tex b/Doc/lib/liboperator.tex
index e3aae60..ab68d0e 100644
--- a/Doc/lib/liboperator.tex
+++ b/Doc/lib/liboperator.tex
@@ -300,6 +300,39 @@ Example: Build a dictionary that maps the ordinals from \code{0} to
\end{verbatim}
+The \module{operator} module also defines tools for generalized attribute
+and item lookups. These are useful for making fast field extractors
+as arguments for \function{map()}, \method{list.sort()},
+\method{itertools.groupby()}, or other functions that expect a
+function argument.
+
+\begin{funcdesc}{attrgetter}{attr}
+Return a callable object that fetches \var{attr} from its operand.
+After, \samp{f=attrgetter('name')}, the call \samp{f(b)} returns
+\samp{b.name}.
+\versionadded{2.4}
+\end{funcdesc}
+
+\begin{funcdesc}{itemgetter}{item}
+Return a callable object that fetches \var{item} from its operand.
+After, \samp{f=itemgetter(2)}, the call \samp{f(b)} returns
+\samp{b[2]}.
+\versionadded{2.4}
+\end{funcdesc}
+
+Examples:
+
+\begin{verbatim}
+>>> from operator import *
+>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
+>>> getcount = itemgetter(1)
+>>> map(getcount, inventory)
+[3, 2, 5, 1]
+>>> list.sorted(inventory, key=getcount)
+[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]
+\end{verbatim}
+
+
\subsection{Mapping Operators to Functions \label{operator-map}}
This table shows how abstract operations correspond to operator