python/dist/src/Doc/lib liboperator.tex, 1.29, 1.30

Update of /cvsroot/python/python/dist/src/Doc/lib In directory sc8-pr-cvs1:/tmp/cvs-serv29352/Doc/lib Modified Files: liboperator.tex Log Message: As discussed on python-dev, added two extractor functions to the operator module. Index: liboperator.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/liboperator.tex,v retrieving revision 1.29 retrieving revision 1.30 diff -C2 -d -r1.29 -r1.30 *** liboperator.tex 27 Nov 2003 19:48:03 -0000 1.29 --- liboperator.tex 1 Dec 2003 13:18:39 -0000 1.30 *************** *** 301,304 **** --- 301,337 ---- + 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}}
participants (1)
-
rhettingerīŧ users.sourceforge.net