blob: 5ac1b6d0636bd3c373521d74da30380f4c5764f1 [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}
45for more informations about rich comparisons.
46\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.
69\end{funcdesc}
70
71\begin{funcdesc}{is_not}{a, b}
72Return \code{\var{a} is not \var{b}}. Tests object identity.
73\end{funcdesc}
74
Guido van Rossum61ed4db1996-12-06 21:22:41 +000075
Fred Drakee89659c2001-08-10 15:55:09 +000076The mathematical and bitwise operations are the most numerous:
Guido van Rossum61ed4db1996-12-06 21:22:41 +000077
78\begin{funcdesc}{abs}{o}
Fred Drakec07ae9f1998-03-08 05:56:15 +000079\funcline{__abs__}{o}
Fred Drake0514ce11997-12-16 14:29:48 +000080Return the absolute value of \var{o}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000081\end{funcdesc}
82
Fred Drakee89659c2001-08-10 15:55:09 +000083\begin{funcdesc}{add}{a, b}
84\funcline{__add__}{a, b}
85Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000086\end{funcdesc}
87
Fred Drake98b032a1997-12-04 14:20:59 +000088\begin{funcdesc}{and_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000089\funcline{__and__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000090Return the bitwise and of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000091\end{funcdesc}
92
Fred Drakee89659c2001-08-10 15:55:09 +000093\begin{funcdesc}{div}{a, b}
94\funcline{__div__}{a, b}
95Return \var{a} \code{/} \var{b} when \code{__future__.division} is not
96in effect. This is also known as ``classic'' division.
97\end{funcdesc}
98
99\begin{funcdesc}{floordiv}{a, b}
100\funcline{__floordiv__}{a, b}
101Return \var{a} \code{//} \var{b}.
102\versionadded{2.2}
103\end{funcdesc}
104
105\begin{funcdesc}{inv}{o}
106\funcline{invert}{o}
107\funcline{__inv__}{o}
108\funcline{__invert__}{o}
109Return the bitwise inverse of the number \var{o}. This is equivalent
110to \code{\textasciitilde}\var{o}. The names \function{invert()} and
111\function{__invert__()} were added in Python 2.0.
112\end{funcdesc}
113
114\begin{funcdesc}{lshift}{a, b}
115\funcline{__lshift__}{a, b}
116Return \var{a} shifted left by \var{b}.
117\end{funcdesc}
118
119\begin{funcdesc}{mod}{a, b}
120\funcline{__mod__}{a, b}
121Return \var{a} \code{\%} \var{b}.
122\end{funcdesc}
123
124\begin{funcdesc}{mul}{a, b}
125\funcline{__mul__}{a, b}
126Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers.
127\end{funcdesc}
128
129\begin{funcdesc}{neg}{o}
130\funcline{__neg__}{o}
131Return \var{o} negated.
132\end{funcdesc}
133
Fred Drake98b032a1997-12-04 14:20:59 +0000134\begin{funcdesc}{or_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000135\funcline{__or__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000136Return the bitwise or of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000137\end{funcdesc}
138
Fred Drakee89659c2001-08-10 15:55:09 +0000139\begin{funcdesc}{pos}{o}
140\funcline{__pos__}{o}
141Return \var{o} positive.
142\end{funcdesc}
143
Raymond Hettinger5959c552002-08-19 03:19:09 +0000144\begin{funcdesc}{pow}{a, b}
145\funcline{__pow__}{a, b}
146Return \var{a} \code{**} \var{b}, for \var{a} and \var{b} numbers.
Neal Norwitz11b795c2002-08-19 22:38:01 +0000147\versionadded{2.3}
Raymond Hettinger5959c552002-08-19 03:19:09 +0000148\end{funcdesc}
149
Fred Drakee89659c2001-08-10 15:55:09 +0000150\begin{funcdesc}{rshift}{a, b}
151\funcline{__rshift__}{a, b}
152Return \var{a} shifted right by \var{b}.
153\end{funcdesc}
154
155\begin{funcdesc}{sub}{a, b}
156\funcline{__sub__}{a, b}
157Return \var{a} \code{-} \var{b}.
158\end{funcdesc}
159
160\begin{funcdesc}{truediv}{a, b}
161\funcline{__truediv__}{a, b}
162Return \var{a} \code{/} \var{b} when \code{__future__.division} is in
163effect. This is also known as division.
164\versionadded{2.2}
165\end{funcdesc}
166
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000167\begin{funcdesc}{xor}{a, b}
168\funcline{__xor__}{a, b}
169Return the bitwise exclusive or of \var{a} and \var{b}.
170\end{funcdesc}
171
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000172
Fred Drakee89659c2001-08-10 15:55:09 +0000173Operations which work with sequences include:
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000174
Fred Drake98b032a1997-12-04 14:20:59 +0000175\begin{funcdesc}{concat}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000176\funcline{__concat__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000177Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000178\end{funcdesc}
179
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000180\begin{funcdesc}{contains}{a, b}
Fred Drake5316ef42000-09-17 16:10:25 +0000181\funcline{__contains__}{a, b}
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000182Return the outcome of the test \var{b} \code{in} \var{a}.
Fred Drake5316ef42000-09-17 16:10:25 +0000183Note the reversed operands. The name \function{__contains__()} was
184added in Python 2.0.
185\end{funcdesc}
186
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000187\begin{funcdesc}{countOf}{a, b}
188Return the number of occurrences of \var{b} in \var{a}.
189\end{funcdesc}
190
Fred Drake98b032a1997-12-04 14:20:59 +0000191\begin{funcdesc}{delitem}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000192\funcline{__delitem__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000193Remove the value of \var{a} at index \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000194\end{funcdesc}
195
Fred Drakee89659c2001-08-10 15:55:09 +0000196\begin{funcdesc}{delslice}{a, b, c}
197\funcline{__delslice__}{a, b, c}
198Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
199\end{funcdesc}
200
201\begin{funcdesc}{getitem}{a, b}
202\funcline{__getitem__}{a, b}
203Return the value of \var{a} at index \var{b}.
204\end{funcdesc}
205
Fred Drake98b032a1997-12-04 14:20:59 +0000206\begin{funcdesc}{getslice}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000207\funcline{__getslice__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000208Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000209\end{funcdesc}
210
Fred Drakee89659c2001-08-10 15:55:09 +0000211\begin{funcdesc}{indexOf}{a, b}
212Return the index of the first of occurrence of \var{b} in \var{a}.
213\end{funcdesc}
214
215\begin{funcdesc}{repeat}{a, b}
216\funcline{__repeat__}{a, b}
217Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
218\var{b} is an integer.
219\end{funcdesc}
220
221\begin{funcdesc}{sequenceIncludes}{\unspecified}
222\deprecated{2.0}{Use \function{contains()} instead.}
223Alias for \function{contains()}.
224\end{funcdesc}
225
226\begin{funcdesc}{setitem}{a, b, c}
227\funcline{__setitem__}{a, b, c}
228Set the value of \var{a} at index \var{b} to \var{c}.
229\end{funcdesc}
230
Fred Drake98b032a1997-12-04 14:20:59 +0000231\begin{funcdesc}{setslice}{a, b, c, v}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000232\funcline{__setslice__}{a, b, c, v}
Fred Drake0514ce11997-12-16 14:29:48 +0000233Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
234sequence \var{v}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000235\end{funcdesc}
236
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000237
Fred Drakee89659c2001-08-10 15:55:09 +0000238The \module{operator} module also defines a few predicates to test the
Fred Drake0aa811c2001-10-20 04:24:09 +0000239type of objects. \note{Be careful not to misinterpret the
Fred Drake8d3312f2000-10-02 03:36:18 +0000240results of these functions; only \function{isCallable()} has any
Fred Drake0aa811c2001-10-20 04:24:09 +0000241measure of reliability with instance objects. For example:}
Fred Drake8d3312f2000-10-02 03:36:18 +0000242
243\begin{verbatim}
244>>> class C:
245... pass
246...
247>>> import operator
248>>> o = C()
249>>> operator.isMappingType(o)
2501
251\end{verbatim}
252
253\begin{funcdesc}{isCallable}{o}
254\deprecated{2.0}{Use the \function{callable()} built-in function instead.}
255Returns true if the object \var{o} can be called like a function,
256otherwise it returns false. True is returned for functions, bound and
257unbound methods, class objects, and instance objects which support the
258\method{__call__()} method.
259\end{funcdesc}
260
261\begin{funcdesc}{isMappingType}{o}
262Returns true if the object \var{o} supports the mapping interface.
263This is true for dictionaries and all instance objects.
Fred Drake0aa811c2001-10-20 04:24:09 +0000264\warning{There is no reliable way to test if an instance
Fred Drake8d3312f2000-10-02 03:36:18 +0000265supports the complete mapping protocol since the interface itself is
266ill-defined. This makes this test less useful than it otherwise might
Fred Drake0aa811c2001-10-20 04:24:09 +0000267be.}
Fred Drake8d3312f2000-10-02 03:36:18 +0000268\end{funcdesc}
269
270\begin{funcdesc}{isNumberType}{o}
271Returns true if the object \var{o} represents a number. This is true
272for all numeric types implemented in C, and for all instance objects.
Fred Drake0aa811c2001-10-20 04:24:09 +0000273\warning{There is no reliable way to test if an instance
Fred Drake8d3312f2000-10-02 03:36:18 +0000274supports the complete numeric interface since the interface itself is
275ill-defined. This makes this test less useful than it otherwise might
Fred Drake0aa811c2001-10-20 04:24:09 +0000276be.}
Fred Drake8d3312f2000-10-02 03:36:18 +0000277\end{funcdesc}
278
279\begin{funcdesc}{isSequenceType}{o}
280Returns true if the object \var{o} supports the sequence protocol.
281This returns true for all objects which define sequence methods in C,
Fred Drake0aa811c2001-10-20 04:24:09 +0000282and for all instance objects. \warning{There is no reliable
Fred Drake8d3312f2000-10-02 03:36:18 +0000283way to test if an instance supports the complete sequence interface
284since the interface itself is ill-defined. This makes this test less
Fred Drake0aa811c2001-10-20 04:24:09 +0000285useful than it otherwise might be.}
Fred Drake8d3312f2000-10-02 03:36:18 +0000286\end{funcdesc}
287
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000288
Fred Drake0514ce11997-12-16 14:29:48 +0000289Example: Build a dictionary that maps the ordinals from \code{0} to
290\code{256} to their character equivalents.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000291
Fred Drake19479911998-02-13 06:58:54 +0000292\begin{verbatim}
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000293>>> import operator
294>>> d = {}
295>>> keys = range(256)
296>>> vals = map(chr, keys)
297>>> map(operator.setitem, [d]*len(keys), keys, vals)
Fred Drake19479911998-02-13 06:58:54 +0000298\end{verbatim}
Fred Drake8c2fd492000-10-22 03:19:30 +0000299
300
301\subsection{Mapping Operators to Functions \label{operator-map}}
302
303This table shows how abstract operations correspond to operator
304symbols in the Python syntax and the functions in the
305\refmodule{operator} module.
306
307
308\begin{tableiii}{l|c|l}{textrm}{Operation}{Syntax}{Function}
309 \lineiii{Addition}{\code{\var{a} + \var{b}}}
310 {\code{add(\var{a}, \var{b})}}
311 \lineiii{Concatenation}{\code{\var{seq1} + \var{seq2}}}
312 {\code{concat(\var{seq1}, \var{seq2})}}
313 \lineiii{Containment Test}{\code{\var{o} in \var{seq}}}
314 {\code{contains(\var{seq}, \var{o})}}
315 \lineiii{Division}{\code{\var{a} / \var{b}}}
Fred Drakee89659c2001-08-10 15:55:09 +0000316 {\code{div(\var{a}, \var{b}) \#} without \code{__future__.division}}
317 \lineiii{Division}{\code{\var{a} / \var{b}}}
318 {\code{truediv(\var{a}, \var{b}) \#} with \code{__future__.division}}
319 \lineiii{Division}{\code{\var{a} // \var{b}}}
320 {\code{floordiv(\var{a}, \var{b})}}
Fred Drake8c2fd492000-10-22 03:19:30 +0000321 \lineiii{Bitwise And}{\code{\var{a} \&\ \var{b}}}
322 {\code{and_(\var{a}, \var{b})}}
323 \lineiii{Bitwise Exclusive Or}{\code{\var{a} \^\ \var{b}}}
324 {\code{xor(\var{a}, \var{b})}}
325 \lineiii{Bitwise Inversion}{\code{\~{} \var{a}}}
326 {\code{invert(\var{a})}}
327 \lineiii{Bitwise Or}{\code{\var{a} | \var{b}}}
328 {\code{or_(\var{a}, \var{b})}}
Raymond Hettinger5959c552002-08-19 03:19:09 +0000329 \lineiii{Exponentiation}{\code{\var{a} ** \var{b}}}
330 {\code{pow(\var{a}, \var{b})}}
Raymond Hettinger1b56de02003-02-21 05:42:13 +0000331 \lineiii{Identity}{\code{\var{a} is \var{b}}}
332 {\code{is_(\var{a}, \var{b})}}
333 \lineiii{Identity}{\code{\var{a} is not \var{b}}}
334 {\code{is_not(\var{a}, \var{b})}}
Fred Drake8c2fd492000-10-22 03:19:30 +0000335 \lineiii{Indexed Assignment}{\code{\var{o}[\var{k}] = \var{v}}}
336 {\code{setitem(\var{o}, \var{k}, \var{v})}}
337 \lineiii{Indexed Deletion}{\code{del \var{o}[\var{k}]}}
338 {\code{delitem(\var{o}, \var{k})}}
339 \lineiii{Indexing}{\code{\var{o}[\var{k}]}}
340 {\code{getitem(\var{o}, \var{k})}}
341 \lineiii{Left Shift}{\code{\var{a} <\code{<} \var{b}}}
342 {\code{lshift(\var{a}, \var{b})}}
343 \lineiii{Modulo}{\code{\var{a} \%\ \var{b}}}
344 {\code{mod(\var{a}, \var{b})}}
345 \lineiii{Multiplication}{\code{\var{a} * \var{b}}}
346 {\code{mul(\var{a}, \var{b})}}
347 \lineiii{Negation (Arithmetic)}{\code{- \var{a}}}
348 {\code{neg(\var{a})}}
349 \lineiii{Negation (Logical)}{\code{not \var{a}}}
350 {\code{not_(\var{a})}}
351 \lineiii{Right Shift}{\code{\var{a} >\code{>} \var{b}}}
352 {\code{rshift(\var{a}, \var{b})}}
353 \lineiii{Sequence Repitition}{\code{\var{seq} * \var{i}}}
354 {\code{repeat(\var{seq}, \var{i})}}
355 \lineiii{Slice Assignment}{\code{\var{seq}[\var{i}:\var{j}]} = \var{values}}
356 {\code{setslice(\var{seq}, \var{i}, \var{j}, \var{values})}}
357 \lineiii{Slice Deletion}{\code{del \var{seq}[\var{i}:\var{j}]}}
358 {\code{delslice(\var{seq}, \var{i}, \var{j})}}
359 \lineiii{Slicing}{\code{\var{seq}[\var{i}:\var{j}]}}
360 {\code{getslice(\var{seq}, \var{i}, \var{j})}}
361 \lineiii{String Formatting}{\code{\var{s} \%\ \var{o}}}
362 {\code{mod(\var{s}, \var{o})}}
363 \lineiii{Subtraction}{\code{\var{a} - \var{b}}}
364 {\code{sub(\var{a}, \var{b})}}
365 \lineiii{Truth Test}{\code{\var{o}}}
366 {\code{truth(\var{o})}}
Fred Drakee89659c2001-08-10 15:55:09 +0000367 \lineiii{Ordering}{\code{\var{a} < \var{b}}}
368 {\code{lt(\var{a}, \var{b})}}
369 \lineiii{Ordering}{\code{\var{a} <= \var{b}}}
370 {\code{le(\var{a}, \var{b})}}
371 \lineiii{Equality}{\code{\var{a} == \var{b}}}
372 {\code{eq(\var{a}, \var{b})}}
373 \lineiii{Difference}{\code{\var{a} != \var{b}}}
374 {\code{ne(\var{a}, \var{b})}}
375 \lineiii{Ordering}{\code{\var{a} >= \var{b}}}
376 {\code{ge(\var{a}, \var{b})}}
377 \lineiii{Ordering}{\code{\var{a} > \var{b}}}
378 {\code{gt(\var{a}, \var{b})}}
Fred Drake8c2fd492000-10-22 03:19:30 +0000379\end{tableiii}