blob: 11e004ab5ded13a8109ba9964c648e7677f15780 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{operator} ---
2 Standard operators as functions.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{operator}
Fred Drake295da241998-08-10 19:42:37 +00004\sectionauthor{Skip Montanaro}{skip@automatrix.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00005
6\modulesynopsis{All Python's standard operators as built-in functions.}
7
Guido van Rossum61ed4db1996-12-06 21:22:41 +00008
Fred Drakec07ae9f1998-03-08 05:56:15 +00009The \module{operator} module exports a set of functions implemented in C
Guido van Rossum61ed4db1996-12-06 21:22:41 +000010corresponding to the intrinsic operators of Python. For example,
Fred Drakef3e6df11997-12-29 17:11:55 +000011\code{operator.add(x, y)} is equivalent to the expression \code{x+y}. The
Guido van Rossum61ed4db1996-12-06 21:22:41 +000012function names are those used for special class methods; variants without
Fred Drake98b032a1997-12-04 14:20:59 +000013leading and trailing \samp{__} are also provided for convenience.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000014
Fred Drakee89659c2001-08-10 15:55:09 +000015The functions fall into categories that perform object comparisons,
16logical operations, mathematical operations, sequence operations, and
17abstract type tests.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000018
Fred Drakee89659c2001-08-10 15:55:09 +000019The object comparison functions are useful for all objects, and are
20named after the rich comparison operators they support:
21
22\begin{funcdesc}{lt}{a, b}
23\funcline{le}{a, b}
24\funcline{eq}{a, b}
25\funcline{ne}{a, b}
26\funcline{ge}{a, b}
27\funcline{gt}{a, b}
28\funcline{__lt__}{a, b}
29\funcline{__le__}{a, b}
30\funcline{__eq__}{a, b}
31\funcline{__ne__}{a, b}
32\funcline{__ge__}{a, b}
33\funcline{__gt__}{a, b}
34Perform ``rich comparisons'' between \var{a} and \var{b}. Specifically,
35\code{lt(\var{a}, \var{b})} is equivalent to \code{\var{a} < \var{b}},
36\code{le(\var{a}, \var{b})} is equivalent to \code{\var{a} <= \var{b}},
37\code{eq(\var{a}, \var{b})} is equivalent to \code{\var{a} == \var{b}},
38\code{ne(\var{a}, \var{b})} is equivalent to \code{\var{a} != \var{b}},
39\code{gt(\var{a}, \var{b})} is equivalent to \code{\var{a} > \var{b}}
40and
41\code{ge(\var{a}, \var{b})} is equivalent to \code{\var{a} >= \var{b}}.
42Note that unlike the built-in \function{cmp()}, these functions can
43return any value, which may or may not be interpretable as a Boolean
44value. See the \citetitle[../ref/ref.html]{Python Reference Manual}
Raymond Hettinger68804312005-01-01 00:28:46 +000045for more information about rich comparisons.
Fred Drakee89659c2001-08-10 15:55:09 +000046\versionadded{2.2}
Guido van Rossum61ed4db1996-12-06 21:22:41 +000047\end{funcdesc}
48
Fred Drakee89659c2001-08-10 15:55:09 +000049
50The logical operations are also generally applicable to all objects,
Raymond Hettinger1b56de02003-02-21 05:42:13 +000051and support truth tests, identity tests, and boolean operations:
Fred Drakee89659c2001-08-10 15:55:09 +000052
53\begin{funcdesc}{not_}{o}
54\funcline{__not__}{o}
55Return the outcome of \keyword{not} \var{o}. (Note that there is no
56\method{__not__()} method for object instances; only the interpreter
57core defines this operation. The result is affected by the
58\method{__nonzero__()} and \method{__len__()} methods.)
Guido van Rossum61ed4db1996-12-06 21:22:41 +000059\end{funcdesc}
60
Fred Drakee89659c2001-08-10 15:55:09 +000061\begin{funcdesc}{truth}{o}
Fred Drake8ec17a02003-01-11 23:15:47 +000062Return \constant{True} if \var{o} is true, and \constant{False}
Neal Norwitz06daee92003-01-12 15:04:54 +000063otherwise. This is equivalent to using the \class{bool}
Fred Drake8ec17a02003-01-11 23:15:47 +000064constructor.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000065\end{funcdesc}
66
Raymond Hettinger9543b342003-01-18 23:22:20 +000067\begin{funcdesc}{is_}{a, b}
68Return \code{\var{a} is \var{b}}. Tests object identity.
Raymond Hettinger83c18742003-11-02 09:50:56 +000069\versionadded{2.3}
Raymond Hettinger9543b342003-01-18 23:22:20 +000070\end{funcdesc}
71
72\begin{funcdesc}{is_not}{a, b}
73Return \code{\var{a} is not \var{b}}. Tests object identity.
Raymond Hettinger83c18742003-11-02 09:50:56 +000074\versionadded{2.3}
Raymond Hettinger9543b342003-01-18 23:22:20 +000075\end{funcdesc}
76
Guido van Rossum61ed4db1996-12-06 21:22:41 +000077
Fred Drakee89659c2001-08-10 15:55:09 +000078The mathematical and bitwise operations are the most numerous:
Guido van Rossum61ed4db1996-12-06 21:22:41 +000079
80\begin{funcdesc}{abs}{o}
Fred Drakec07ae9f1998-03-08 05:56:15 +000081\funcline{__abs__}{o}
Fred Drake0514ce11997-12-16 14:29:48 +000082Return the absolute value of \var{o}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000083\end{funcdesc}
84
Fred Drakee89659c2001-08-10 15:55:09 +000085\begin{funcdesc}{add}{a, b}
86\funcline{__add__}{a, b}
87Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000088\end{funcdesc}
89
Fred Drake98b032a1997-12-04 14:20:59 +000090\begin{funcdesc}{and_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000091\funcline{__and__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000092Return the bitwise and of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000093\end{funcdesc}
94
Fred Drakee89659c2001-08-10 15:55:09 +000095\begin{funcdesc}{div}{a, b}
96\funcline{__div__}{a, b}
97Return \var{a} \code{/} \var{b} when \code{__future__.division} is not
98in effect. This is also known as ``classic'' division.
99\end{funcdesc}
100
101\begin{funcdesc}{floordiv}{a, b}
102\funcline{__floordiv__}{a, b}
103Return \var{a} \code{//} \var{b}.
104\versionadded{2.2}
105\end{funcdesc}
106
107\begin{funcdesc}{inv}{o}
108\funcline{invert}{o}
109\funcline{__inv__}{o}
110\funcline{__invert__}{o}
111Return the bitwise inverse of the number \var{o}. This is equivalent
112to \code{\textasciitilde}\var{o}. The names \function{invert()} and
113\function{__invert__()} were added in Python 2.0.
114\end{funcdesc}
115
116\begin{funcdesc}{lshift}{a, b}
117\funcline{__lshift__}{a, b}
118Return \var{a} shifted left by \var{b}.
119\end{funcdesc}
120
121\begin{funcdesc}{mod}{a, b}
122\funcline{__mod__}{a, b}
123Return \var{a} \code{\%} \var{b}.
124\end{funcdesc}
125
126\begin{funcdesc}{mul}{a, b}
127\funcline{__mul__}{a, b}
128Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers.
129\end{funcdesc}
130
131\begin{funcdesc}{neg}{o}
132\funcline{__neg__}{o}
133Return \var{o} negated.
134\end{funcdesc}
135
Fred Drake98b032a1997-12-04 14:20:59 +0000136\begin{funcdesc}{or_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000137\funcline{__or__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000138Return the bitwise or of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000139\end{funcdesc}
140
Fred Drakee89659c2001-08-10 15:55:09 +0000141\begin{funcdesc}{pos}{o}
142\funcline{__pos__}{o}
143Return \var{o} positive.
144\end{funcdesc}
145
Raymond Hettinger5959c552002-08-19 03:19:09 +0000146\begin{funcdesc}{pow}{a, b}
147\funcline{__pow__}{a, b}
148Return \var{a} \code{**} \var{b}, for \var{a} and \var{b} numbers.
Neal Norwitz11b795c2002-08-19 22:38:01 +0000149\versionadded{2.3}
Raymond Hettinger5959c552002-08-19 03:19:09 +0000150\end{funcdesc}
151
Fred Drakee89659c2001-08-10 15:55:09 +0000152\begin{funcdesc}{rshift}{a, b}
153\funcline{__rshift__}{a, b}
154Return \var{a} shifted right by \var{b}.
155\end{funcdesc}
156
157\begin{funcdesc}{sub}{a, b}
158\funcline{__sub__}{a, b}
159Return \var{a} \code{-} \var{b}.
160\end{funcdesc}
161
162\begin{funcdesc}{truediv}{a, b}
163\funcline{__truediv__}{a, b}
164Return \var{a} \code{/} \var{b} when \code{__future__.division} is in
Armin Rigoecc275b2005-12-29 16:04:25 +0000165effect. This is also known as ``true'' division.
Fred Drakee89659c2001-08-10 15:55:09 +0000166\versionadded{2.2}
167\end{funcdesc}
168
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000169\begin{funcdesc}{xor}{a, b}
170\funcline{__xor__}{a, b}
171Return the bitwise exclusive or of \var{a} and \var{b}.
172\end{funcdesc}
173
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000174
Fred Drakee89659c2001-08-10 15:55:09 +0000175Operations which work with sequences include:
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000176
Fred Drake98b032a1997-12-04 14:20:59 +0000177\begin{funcdesc}{concat}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000178\funcline{__concat__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000179Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000180\end{funcdesc}
181
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000182\begin{funcdesc}{contains}{a, b}
Fred Drake5316ef42000-09-17 16:10:25 +0000183\funcline{__contains__}{a, b}
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000184Return the outcome of the test \var{b} \code{in} \var{a}.
Fred Drake5316ef42000-09-17 16:10:25 +0000185Note the reversed operands. The name \function{__contains__()} was
186added in Python 2.0.
187\end{funcdesc}
188
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000189\begin{funcdesc}{countOf}{a, b}
190Return the number of occurrences of \var{b} in \var{a}.
191\end{funcdesc}
192
Fred Drake98b032a1997-12-04 14:20:59 +0000193\begin{funcdesc}{delitem}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000194\funcline{__delitem__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000195Remove the value of \var{a} at index \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000196\end{funcdesc}
197
Fred Drakee89659c2001-08-10 15:55:09 +0000198\begin{funcdesc}{delslice}{a, b, c}
199\funcline{__delslice__}{a, b, c}
200Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
201\end{funcdesc}
202
203\begin{funcdesc}{getitem}{a, b}
204\funcline{__getitem__}{a, b}
205Return the value of \var{a} at index \var{b}.
206\end{funcdesc}
207
Fred Drake98b032a1997-12-04 14:20:59 +0000208\begin{funcdesc}{getslice}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000209\funcline{__getslice__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000210Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000211\end{funcdesc}
212
Fred Drakee89659c2001-08-10 15:55:09 +0000213\begin{funcdesc}{indexOf}{a, b}
214Return the index of the first of occurrence of \var{b} in \var{a}.
215\end{funcdesc}
216
217\begin{funcdesc}{repeat}{a, b}
218\funcline{__repeat__}{a, b}
219Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
220\var{b} is an integer.
221\end{funcdesc}
222
223\begin{funcdesc}{sequenceIncludes}{\unspecified}
224\deprecated{2.0}{Use \function{contains()} instead.}
225Alias for \function{contains()}.
226\end{funcdesc}
227
228\begin{funcdesc}{setitem}{a, b, c}
229\funcline{__setitem__}{a, b, c}
230Set the value of \var{a} at index \var{b} to \var{c}.
231\end{funcdesc}
232
Fred Drake98b032a1997-12-04 14:20:59 +0000233\begin{funcdesc}{setslice}{a, b, c, v}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000234\funcline{__setslice__}{a, b, c, v}
Fred Drake0514ce11997-12-16 14:29:48 +0000235Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
236sequence \var{v}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000237\end{funcdesc}
238
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000239
Armin Rigof5bd3b42005-12-29 16:50:42 +0000240Many operations have an ``in-place'' version. The following functions
241provide a more primitive access to in-place operators than the usual
242syntax does; for example, the statement \code{x += y} is equivalent to
243\code{x = operator.iadd(x, y)}. Another way to put it is to say that
244\code{z = operator.iadd(x, y)} is equivalent to the compound statement
245\code{z = x; z += y}.
246
247\begin{funcdesc}{iadd}{a, b}
248\funcline{__iadd__}{a, b}
249\code{a = iadd(a, b)} is equivalent to \code{a += b}.
250\versionadded{2.5}
251\end{funcdesc}
252
253\begin{funcdesc}{iand}{a, b}
254\funcline{__iand__}{a, b}
255\code{a = iand(a, b)} is equivalent to \code{a \&= b}.
256\versionadded{2.5}
257\end{funcdesc}
258
259\begin{funcdesc}{iconcat}{a, b}
260\funcline{__iconcat__}{a, b}
261\code{a = iconcat(a, b)} is equivalent to \code{a += b} for \var{a}
262and \var{b} sequences.
263\versionadded{2.5}
264\end{funcdesc}
265
266\begin{funcdesc}{idiv}{a, b}
267\funcline{__idiv__}{a, b}
268\code{a = idiv(a, b)} is equivalent to \code{a /= b} when
269\code{__future__.division} is not in effect.
270\versionadded{2.5}
271\end{funcdesc}
272
273\begin{funcdesc}{ifloordiv}{a, b}
274\funcline{__ifloordiv__}{a, b}
275\code{a = ifloordiv(a, b)} is equivalent to \code{a //= b}.
276\versionadded{2.5}
277\end{funcdesc}
278
279\begin{funcdesc}{ilshift}{a, b}
280\funcline{__ilshift__}{a, b}
281\code{a = ilshift(a, b)} is equivalent to \code{a <}\code{<= b}.
282\versionadded{2.5}
283\end{funcdesc}
284
285\begin{funcdesc}{imod}{a, b}
286\funcline{__imod__}{a, b}
287\code{a = imod(a, b)} is equivalent to \code{a \%= b}.
288\versionadded{2.5}
289\end{funcdesc}
290
291\begin{funcdesc}{imul}{a, b}
292\funcline{__imul__}{a, b}
293\code{a = imul(a, b)} is equivalent to \code{a *= b}.
294\versionadded{2.5}
295\end{funcdesc}
296
297\begin{funcdesc}{ior}{a, b}
298\funcline{__ior__}{a, b}
299\code{a = ior(a, b)} is equivalent to \code{a |= b}.
300\versionadded{2.5}
301\end{funcdesc}
302
303\begin{funcdesc}{ipow}{a, b}
304\funcline{__ipow__}{a, b}
305\code{a = ipow(a, b)} is equivalent to \code{a **= b}.
306\versionadded{2.5}
307\end{funcdesc}
308
309\begin{funcdesc}{irepeat}{a, b}
310\funcline{__irepeat__}{a, b}
311\code{a = irepeat(a, b)} is equivalent to \code{a *= b} where
312\var{a} is a sequence and \var{b} is an integer.
313\versionadded{2.5}
314\end{funcdesc}
315
316\begin{funcdesc}{irshift}{a, b}
317\funcline{__irshift__}{a, b}
318\code{a = irshift(a, b)} is equivalent to \code{a >}\code{>= b}.
319\versionadded{2.5}
320\end{funcdesc}
321
322\begin{funcdesc}{isub}{a, b}
323\funcline{__isub__}{a, b}
324\code{a = isub(a, b)} is equivalent to \code{a -= b}.
325\versionadded{2.5}
326\end{funcdesc}
327
328\begin{funcdesc}{itruediv}{a, b}
329\funcline{__itruediv__}{a, b}
330\code{a = itruediv(a, b)} is equivalent to \code{a /= b} when
331\code{__future__.division} is in effect.
332\versionadded{2.5}
333\end{funcdesc}
334
335\begin{funcdesc}{ixor}{a, b}
336\funcline{__ixor__}{a, b}
337\code{a = ixor(a, b)} is equivalent to \code{a \textasciicircum= b}.
338\versionadded{2.5}
339\end{funcdesc}
340
341
Fred Drakee89659c2001-08-10 15:55:09 +0000342The \module{operator} module also defines a few predicates to test the
Fred Drake0aa811c2001-10-20 04:24:09 +0000343type of objects. \note{Be careful not to misinterpret the
Fred Drake8d3312f2000-10-02 03:36:18 +0000344results of these functions; only \function{isCallable()} has any
Fred Drake0aa811c2001-10-20 04:24:09 +0000345measure of reliability with instance objects. For example:}
Fred Drake8d3312f2000-10-02 03:36:18 +0000346
347\begin{verbatim}
348>>> class C:
349... pass
350...
351>>> import operator
352>>> o = C()
353>>> operator.isMappingType(o)
Martin v. Löwisccabed32003-11-27 19:48:03 +0000354True
Fred Drake8d3312f2000-10-02 03:36:18 +0000355\end{verbatim}
356
357\begin{funcdesc}{isCallable}{o}
358\deprecated{2.0}{Use the \function{callable()} built-in function instead.}
359Returns true if the object \var{o} can be called like a function,
360otherwise it returns false. True is returned for functions, bound and
361unbound methods, class objects, and instance objects which support the
362\method{__call__()} method.
363\end{funcdesc}
364
365\begin{funcdesc}{isMappingType}{o}
366Returns true if the object \var{o} supports the mapping interface.
Raymond Hettingerad351f82005-04-07 05:36:17 +0000367This is true for dictionaries and all instance objects defining
368\method{__getitem__}.
Fred Drake0aa811c2001-10-20 04:24:09 +0000369\warning{There is no reliable way to test if an instance
Fred Drake8d3312f2000-10-02 03:36:18 +0000370supports the complete mapping protocol since the interface itself is
371ill-defined. This makes this test less useful than it otherwise might
Fred Drake0aa811c2001-10-20 04:24:09 +0000372be.}
Fred Drake8d3312f2000-10-02 03:36:18 +0000373\end{funcdesc}
374
375\begin{funcdesc}{isNumberType}{o}
376Returns true if the object \var{o} represents a number. This is true
Raymond Hettingerad351f82005-04-07 05:36:17 +0000377for all numeric types implemented in C.
Fred Drake0aa811c2001-10-20 04:24:09 +0000378\warning{There is no reliable way to test if an instance
Fred Drake8d3312f2000-10-02 03:36:18 +0000379supports the complete numeric interface since the interface itself is
380ill-defined. This makes this test less useful than it otherwise might
Fred Drake0aa811c2001-10-20 04:24:09 +0000381be.}
Fred Drake8d3312f2000-10-02 03:36:18 +0000382\end{funcdesc}
383
384\begin{funcdesc}{isSequenceType}{o}
385Returns true if the object \var{o} supports the sequence protocol.
386This returns true for all objects which define sequence methods in C,
Raymond Hettingerad351f82005-04-07 05:36:17 +0000387and for all instance objects defining \method{__getitem__}.
388\warning{There is no reliable
Fred Drake8d3312f2000-10-02 03:36:18 +0000389way to test if an instance supports the complete sequence interface
390since the interface itself is ill-defined. This makes this test less
Fred Drake0aa811c2001-10-20 04:24:09 +0000391useful than it otherwise might be.}
Fred Drake8d3312f2000-10-02 03:36:18 +0000392\end{funcdesc}
393
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000394
Fred Drake0514ce11997-12-16 14:29:48 +0000395Example: Build a dictionary that maps the ordinals from \code{0} to
Raymond Hettingerfb5f04d2005-04-07 04:38:04 +0000396\code{255} to their character equivalents.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000397
Fred Drake19479911998-02-13 06:58:54 +0000398\begin{verbatim}
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000399>>> import operator
400>>> d = {}
401>>> keys = range(256)
402>>> vals = map(chr, keys)
403>>> map(operator.setitem, [d]*len(keys), keys, vals)
Fred Drake19479911998-02-13 06:58:54 +0000404\end{verbatim}
Fred Drake8c2fd492000-10-22 03:19:30 +0000405
406
Raymond Hettinger166958b2003-12-01 13:18:39 +0000407The \module{operator} module also defines tools for generalized attribute
408and item lookups. These are useful for making fast field extractors
Raymond Hettingerb606b3d2003-12-17 20:50:46 +0000409as arguments for \function{map()}, \function{sorted()},
Raymond Hettinger166958b2003-12-01 13:18:39 +0000410\method{itertools.groupby()}, or other functions that expect a
411function argument.
412
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000413\begin{funcdesc}{attrgetter}{attr\optional{, args...}}
Raymond Hettinger166958b2003-12-01 13:18:39 +0000414Return a callable object that fetches \var{attr} from its operand.
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000415If more than one attribute is requested, returns a tuple of attributes.
Raymond Hettinger166958b2003-12-01 13:18:39 +0000416After, \samp{f=attrgetter('name')}, the call \samp{f(b)} returns
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000417\samp{b.name}. After, \samp{f=attrgetter('name', 'date')}, the call
418\samp{f(b)} returns \samp{(b.name, b.date)}.
Raymond Hettinger166958b2003-12-01 13:18:39 +0000419\versionadded{2.4}
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000420\versionchanged[Added support for multiple attributes]{2.5}
Raymond Hettinger166958b2003-12-01 13:18:39 +0000421\end{funcdesc}
422
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000423\begin{funcdesc}{itemgetter}{item\optional{, args...}}
Raymond Hettinger166958b2003-12-01 13:18:39 +0000424Return a callable object that fetches \var{item} from its operand.
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000425If more than one item is requested, returns a tuple of items.
Raymond Hettinger166958b2003-12-01 13:18:39 +0000426After, \samp{f=itemgetter(2)}, the call \samp{f(b)} returns
427\samp{b[2]}.
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000428After, \samp{f=itemgetter(2,5,3)}, the call \samp{f(b)} returns
429\samp{(b[2], b[5], b[3])}.
Raymond Hettinger166958b2003-12-01 13:18:39 +0000430\versionadded{2.4}
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000431\versionchanged[Added support for multiple item extraction]{2.5}
Raymond Hettinger166958b2003-12-01 13:18:39 +0000432\end{funcdesc}
433
434Examples:
435
436\begin{verbatim}
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000437>>> from operator import itemgetter
Raymond Hettinger166958b2003-12-01 13:18:39 +0000438>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
439>>> getcount = itemgetter(1)
440>>> map(getcount, inventory)
441[3, 2, 5, 1]
Raymond Hettingerb606b3d2003-12-17 20:50:46 +0000442>>> sorted(inventory, key=getcount)
Raymond Hettinger166958b2003-12-01 13:18:39 +0000443[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]
444\end{verbatim}
445
446
Fred Drake8c2fd492000-10-22 03:19:30 +0000447\subsection{Mapping Operators to Functions \label{operator-map}}
448
449This table shows how abstract operations correspond to operator
450symbols in the Python syntax and the functions in the
451\refmodule{operator} module.
452
453
454\begin{tableiii}{l|c|l}{textrm}{Operation}{Syntax}{Function}
455 \lineiii{Addition}{\code{\var{a} + \var{b}}}
456 {\code{add(\var{a}, \var{b})}}
457 \lineiii{Concatenation}{\code{\var{seq1} + \var{seq2}}}
458 {\code{concat(\var{seq1}, \var{seq2})}}
459 \lineiii{Containment Test}{\code{\var{o} in \var{seq}}}
460 {\code{contains(\var{seq}, \var{o})}}
461 \lineiii{Division}{\code{\var{a} / \var{b}}}
Fred Drakee89659c2001-08-10 15:55:09 +0000462 {\code{div(\var{a}, \var{b}) \#} without \code{__future__.division}}
463 \lineiii{Division}{\code{\var{a} / \var{b}}}
464 {\code{truediv(\var{a}, \var{b}) \#} with \code{__future__.division}}
465 \lineiii{Division}{\code{\var{a} // \var{b}}}
466 {\code{floordiv(\var{a}, \var{b})}}
Fred Drake8c2fd492000-10-22 03:19:30 +0000467 \lineiii{Bitwise And}{\code{\var{a} \&\ \var{b}}}
468 {\code{and_(\var{a}, \var{b})}}
469 \lineiii{Bitwise Exclusive Or}{\code{\var{a} \^\ \var{b}}}
470 {\code{xor(\var{a}, \var{b})}}
471 \lineiii{Bitwise Inversion}{\code{\~{} \var{a}}}
472 {\code{invert(\var{a})}}
473 \lineiii{Bitwise Or}{\code{\var{a} | \var{b}}}
474 {\code{or_(\var{a}, \var{b})}}
Raymond Hettinger5959c552002-08-19 03:19:09 +0000475 \lineiii{Exponentiation}{\code{\var{a} ** \var{b}}}
476 {\code{pow(\var{a}, \var{b})}}
Raymond Hettinger1b56de02003-02-21 05:42:13 +0000477 \lineiii{Identity}{\code{\var{a} is \var{b}}}
478 {\code{is_(\var{a}, \var{b})}}
479 \lineiii{Identity}{\code{\var{a} is not \var{b}}}
480 {\code{is_not(\var{a}, \var{b})}}
Fred Drake8c2fd492000-10-22 03:19:30 +0000481 \lineiii{Indexed Assignment}{\code{\var{o}[\var{k}] = \var{v}}}
482 {\code{setitem(\var{o}, \var{k}, \var{v})}}
483 \lineiii{Indexed Deletion}{\code{del \var{o}[\var{k}]}}
484 {\code{delitem(\var{o}, \var{k})}}
485 \lineiii{Indexing}{\code{\var{o}[\var{k}]}}
486 {\code{getitem(\var{o}, \var{k})}}
487 \lineiii{Left Shift}{\code{\var{a} <\code{<} \var{b}}}
488 {\code{lshift(\var{a}, \var{b})}}
489 \lineiii{Modulo}{\code{\var{a} \%\ \var{b}}}
490 {\code{mod(\var{a}, \var{b})}}
491 \lineiii{Multiplication}{\code{\var{a} * \var{b}}}
492 {\code{mul(\var{a}, \var{b})}}
493 \lineiii{Negation (Arithmetic)}{\code{- \var{a}}}
494 {\code{neg(\var{a})}}
495 \lineiii{Negation (Logical)}{\code{not \var{a}}}
496 {\code{not_(\var{a})}}
497 \lineiii{Right Shift}{\code{\var{a} >\code{>} \var{b}}}
498 {\code{rshift(\var{a}, \var{b})}}
499 \lineiii{Sequence Repitition}{\code{\var{seq} * \var{i}}}
500 {\code{repeat(\var{seq}, \var{i})}}
501 \lineiii{Slice Assignment}{\code{\var{seq}[\var{i}:\var{j}]} = \var{values}}
502 {\code{setslice(\var{seq}, \var{i}, \var{j}, \var{values})}}
503 \lineiii{Slice Deletion}{\code{del \var{seq}[\var{i}:\var{j}]}}
504 {\code{delslice(\var{seq}, \var{i}, \var{j})}}
505 \lineiii{Slicing}{\code{\var{seq}[\var{i}:\var{j}]}}
506 {\code{getslice(\var{seq}, \var{i}, \var{j})}}
507 \lineiii{String Formatting}{\code{\var{s} \%\ \var{o}}}
508 {\code{mod(\var{s}, \var{o})}}
509 \lineiii{Subtraction}{\code{\var{a} - \var{b}}}
510 {\code{sub(\var{a}, \var{b})}}
511 \lineiii{Truth Test}{\code{\var{o}}}
512 {\code{truth(\var{o})}}
Fred Drakee89659c2001-08-10 15:55:09 +0000513 \lineiii{Ordering}{\code{\var{a} < \var{b}}}
514 {\code{lt(\var{a}, \var{b})}}
515 \lineiii{Ordering}{\code{\var{a} <= \var{b}}}
516 {\code{le(\var{a}, \var{b})}}
517 \lineiii{Equality}{\code{\var{a} == \var{b}}}
518 {\code{eq(\var{a}, \var{b})}}
519 \lineiii{Difference}{\code{\var{a} != \var{b}}}
520 {\code{ne(\var{a}, \var{b})}}
521 \lineiii{Ordering}{\code{\var{a} >= \var{b}}}
522 {\code{ge(\var{a}, \var{b})}}
523 \lineiii{Ordering}{\code{\var{a} > \var{b}}}
524 {\code{gt(\var{a}, \var{b})}}
Fred Drake8c2fd492000-10-22 03:19:30 +0000525\end{tableiii}