blob: 7d34a0f2b9428db9b15a68f5ca2ec0ced27ea550 [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,
51and support truth tests and Boolean operations:
52
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}
62Return \code{1} if \var{o} is true, and 0 otherwise.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000063\end{funcdesc}
64
Guido van Rossum61ed4db1996-12-06 21:22:41 +000065
Fred Drakee89659c2001-08-10 15:55:09 +000066The mathematical and bitwise operations are the most numerous:
Guido van Rossum61ed4db1996-12-06 21:22:41 +000067
68\begin{funcdesc}{abs}{o}
Fred Drakec07ae9f1998-03-08 05:56:15 +000069\funcline{__abs__}{o}
Fred Drake0514ce11997-12-16 14:29:48 +000070Return the absolute value of \var{o}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000071\end{funcdesc}
72
Fred Drakee89659c2001-08-10 15:55:09 +000073\begin{funcdesc}{add}{a, b}
74\funcline{__add__}{a, b}
75Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000076\end{funcdesc}
77
Fred Drake98b032a1997-12-04 14:20:59 +000078\begin{funcdesc}{and_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000079\funcline{__and__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000080Return the bitwise and of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000081\end{funcdesc}
82
Fred Drakee89659c2001-08-10 15:55:09 +000083\begin{funcdesc}{div}{a, b}
84\funcline{__div__}{a, b}
85Return \var{a} \code{/} \var{b} when \code{__future__.division} is not
86in effect. This is also known as ``classic'' division.
87\end{funcdesc}
88
89\begin{funcdesc}{floordiv}{a, b}
90\funcline{__floordiv__}{a, b}
91Return \var{a} \code{//} \var{b}.
92\versionadded{2.2}
93\end{funcdesc}
94
95\begin{funcdesc}{inv}{o}
96\funcline{invert}{o}
97\funcline{__inv__}{o}
98\funcline{__invert__}{o}
99Return the bitwise inverse of the number \var{o}. This is equivalent
100to \code{\textasciitilde}\var{o}. The names \function{invert()} and
101\function{__invert__()} were added in Python 2.0.
102\end{funcdesc}
103
104\begin{funcdesc}{lshift}{a, b}
105\funcline{__lshift__}{a, b}
106Return \var{a} shifted left by \var{b}.
107\end{funcdesc}
108
109\begin{funcdesc}{mod}{a, b}
110\funcline{__mod__}{a, b}
111Return \var{a} \code{\%} \var{b}.
112\end{funcdesc}
113
114\begin{funcdesc}{mul}{a, b}
115\funcline{__mul__}{a, b}
116Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers.
117\end{funcdesc}
118
119\begin{funcdesc}{neg}{o}
120\funcline{__neg__}{o}
121Return \var{o} negated.
122\end{funcdesc}
123
Fred Drake98b032a1997-12-04 14:20:59 +0000124\begin{funcdesc}{or_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000125\funcline{__or__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000126Return the bitwise or of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000127\end{funcdesc}
128
Fred Drakee89659c2001-08-10 15:55:09 +0000129\begin{funcdesc}{pos}{o}
130\funcline{__pos__}{o}
131Return \var{o} positive.
132\end{funcdesc}
133
134\begin{funcdesc}{rshift}{a, b}
135\funcline{__rshift__}{a, b}
136Return \var{a} shifted right by \var{b}.
137\end{funcdesc}
138
139\begin{funcdesc}{sub}{a, b}
140\funcline{__sub__}{a, b}
141Return \var{a} \code{-} \var{b}.
142\end{funcdesc}
143
144\begin{funcdesc}{truediv}{a, b}
145\funcline{__truediv__}{a, b}
146Return \var{a} \code{/} \var{b} when \code{__future__.division} is in
147effect. This is also known as division.
148\versionadded{2.2}
149\end{funcdesc}
150
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000151\begin{funcdesc}{xor}{a, b}
152\funcline{__xor__}{a, b}
153Return the bitwise exclusive or of \var{a} and \var{b}.
154\end{funcdesc}
155
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000156
Fred Drakee89659c2001-08-10 15:55:09 +0000157Operations which work with sequences include:
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000158
Fred Drake98b032a1997-12-04 14:20:59 +0000159\begin{funcdesc}{concat}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000160\funcline{__concat__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000161Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000162\end{funcdesc}
163
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000164\begin{funcdesc}{contains}{a, b}
Fred Drake5316ef42000-09-17 16:10:25 +0000165\funcline{__contains__}{a, b}
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000166Return the outcome of the test \var{b} \code{in} \var{a}.
Fred Drake5316ef42000-09-17 16:10:25 +0000167Note the reversed operands. The name \function{__contains__()} was
168added in Python 2.0.
169\end{funcdesc}
170
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000171\begin{funcdesc}{countOf}{a, b}
172Return the number of occurrences of \var{b} in \var{a}.
173\end{funcdesc}
174
Fred Drake98b032a1997-12-04 14:20:59 +0000175\begin{funcdesc}{delitem}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000176\funcline{__delitem__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000177Remove the value of \var{a} at index \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000178\end{funcdesc}
179
Fred Drakee89659c2001-08-10 15:55:09 +0000180\begin{funcdesc}{delslice}{a, b, c}
181\funcline{__delslice__}{a, b, c}
182Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
183\end{funcdesc}
184
185\begin{funcdesc}{getitem}{a, b}
186\funcline{__getitem__}{a, b}
187Return the value of \var{a} at index \var{b}.
188\end{funcdesc}
189
Fred Drake98b032a1997-12-04 14:20:59 +0000190\begin{funcdesc}{getslice}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000191\funcline{__getslice__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000192Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000193\end{funcdesc}
194
Fred Drakee89659c2001-08-10 15:55:09 +0000195\begin{funcdesc}{indexOf}{a, b}
196Return the index of the first of occurrence of \var{b} in \var{a}.
197\end{funcdesc}
198
199\begin{funcdesc}{repeat}{a, b}
200\funcline{__repeat__}{a, b}
201Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
202\var{b} is an integer.
203\end{funcdesc}
204
205\begin{funcdesc}{sequenceIncludes}{\unspecified}
206\deprecated{2.0}{Use \function{contains()} instead.}
207Alias for \function{contains()}.
208\end{funcdesc}
209
210\begin{funcdesc}{setitem}{a, b, c}
211\funcline{__setitem__}{a, b, c}
212Set the value of \var{a} at index \var{b} to \var{c}.
213\end{funcdesc}
214
Fred Drake98b032a1997-12-04 14:20:59 +0000215\begin{funcdesc}{setslice}{a, b, c, v}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000216\funcline{__setslice__}{a, b, c, v}
Fred Drake0514ce11997-12-16 14:29:48 +0000217Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
218sequence \var{v}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000219\end{funcdesc}
220
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000221
Fred Drakee89659c2001-08-10 15:55:09 +0000222The \module{operator} module also defines a few predicates to test the
Fred Drake0aa811c2001-10-20 04:24:09 +0000223type of objects. \note{Be careful not to misinterpret the
Fred Drake8d3312f2000-10-02 03:36:18 +0000224results of these functions; only \function{isCallable()} has any
Fred Drake0aa811c2001-10-20 04:24:09 +0000225measure of reliability with instance objects. For example:}
Fred Drake8d3312f2000-10-02 03:36:18 +0000226
227\begin{verbatim}
228>>> class C:
229... pass
230...
231>>> import operator
232>>> o = C()
233>>> operator.isMappingType(o)
2341
235\end{verbatim}
236
237\begin{funcdesc}{isCallable}{o}
238\deprecated{2.0}{Use the \function{callable()} built-in function instead.}
239Returns true if the object \var{o} can be called like a function,
240otherwise it returns false. True is returned for functions, bound and
241unbound methods, class objects, and instance objects which support the
242\method{__call__()} method.
243\end{funcdesc}
244
245\begin{funcdesc}{isMappingType}{o}
246Returns true if the object \var{o} supports the mapping interface.
247This is true for dictionaries and all instance objects.
Fred Drake0aa811c2001-10-20 04:24:09 +0000248\warning{There is no reliable way to test if an instance
Fred Drake8d3312f2000-10-02 03:36:18 +0000249supports the complete mapping protocol since the interface itself is
250ill-defined. This makes this test less useful than it otherwise might
Fred Drake0aa811c2001-10-20 04:24:09 +0000251be.}
Fred Drake8d3312f2000-10-02 03:36:18 +0000252\end{funcdesc}
253
254\begin{funcdesc}{isNumberType}{o}
255Returns true if the object \var{o} represents a number. This is true
256for all numeric types implemented in C, and for all instance objects.
Fred Drake0aa811c2001-10-20 04:24:09 +0000257\warning{There is no reliable way to test if an instance
Fred Drake8d3312f2000-10-02 03:36:18 +0000258supports the complete numeric interface since the interface itself is
259ill-defined. This makes this test less useful than it otherwise might
Fred Drake0aa811c2001-10-20 04:24:09 +0000260be.}
Fred Drake8d3312f2000-10-02 03:36:18 +0000261\end{funcdesc}
262
263\begin{funcdesc}{isSequenceType}{o}
264Returns true if the object \var{o} supports the sequence protocol.
265This returns true for all objects which define sequence methods in C,
Fred Drake0aa811c2001-10-20 04:24:09 +0000266and for all instance objects. \warning{There is no reliable
Fred Drake8d3312f2000-10-02 03:36:18 +0000267way to test if an instance supports the complete sequence interface
268since the interface itself is ill-defined. This makes this test less
Fred Drake0aa811c2001-10-20 04:24:09 +0000269useful than it otherwise might be.}
Fred Drake8d3312f2000-10-02 03:36:18 +0000270\end{funcdesc}
271
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000272
Fred Drake0514ce11997-12-16 14:29:48 +0000273Example: Build a dictionary that maps the ordinals from \code{0} to
274\code{256} to their character equivalents.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000275
Fred Drake19479911998-02-13 06:58:54 +0000276\begin{verbatim}
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000277>>> import operator
278>>> d = {}
279>>> keys = range(256)
280>>> vals = map(chr, keys)
281>>> map(operator.setitem, [d]*len(keys), keys, vals)
Fred Drake19479911998-02-13 06:58:54 +0000282\end{verbatim}
Fred Drake8c2fd492000-10-22 03:19:30 +0000283
284
285\subsection{Mapping Operators to Functions \label{operator-map}}
286
287This table shows how abstract operations correspond to operator
288symbols in the Python syntax and the functions in the
289\refmodule{operator} module.
290
291
292\begin{tableiii}{l|c|l}{textrm}{Operation}{Syntax}{Function}
293 \lineiii{Addition}{\code{\var{a} + \var{b}}}
294 {\code{add(\var{a}, \var{b})}}
295 \lineiii{Concatenation}{\code{\var{seq1} + \var{seq2}}}
296 {\code{concat(\var{seq1}, \var{seq2})}}
297 \lineiii{Containment Test}{\code{\var{o} in \var{seq}}}
298 {\code{contains(\var{seq}, \var{o})}}
299 \lineiii{Division}{\code{\var{a} / \var{b}}}
Fred Drakee89659c2001-08-10 15:55:09 +0000300 {\code{div(\var{a}, \var{b}) \#} without \code{__future__.division}}
301 \lineiii{Division}{\code{\var{a} / \var{b}}}
302 {\code{truediv(\var{a}, \var{b}) \#} with \code{__future__.division}}
303 \lineiii{Division}{\code{\var{a} // \var{b}}}
304 {\code{floordiv(\var{a}, \var{b})}}
Fred Drake8c2fd492000-10-22 03:19:30 +0000305 \lineiii{Bitwise And}{\code{\var{a} \&\ \var{b}}}
306 {\code{and_(\var{a}, \var{b})}}
307 \lineiii{Bitwise Exclusive Or}{\code{\var{a} \^\ \var{b}}}
308 {\code{xor(\var{a}, \var{b})}}
309 \lineiii{Bitwise Inversion}{\code{\~{} \var{a}}}
310 {\code{invert(\var{a})}}
311 \lineiii{Bitwise Or}{\code{\var{a} | \var{b}}}
312 {\code{or_(\var{a}, \var{b})}}
313 \lineiii{Indexed Assignment}{\code{\var{o}[\var{k}] = \var{v}}}
314 {\code{setitem(\var{o}, \var{k}, \var{v})}}
315 \lineiii{Indexed Deletion}{\code{del \var{o}[\var{k}]}}
316 {\code{delitem(\var{o}, \var{k})}}
317 \lineiii{Indexing}{\code{\var{o}[\var{k}]}}
318 {\code{getitem(\var{o}, \var{k})}}
319 \lineiii{Left Shift}{\code{\var{a} <\code{<} \var{b}}}
320 {\code{lshift(\var{a}, \var{b})}}
321 \lineiii{Modulo}{\code{\var{a} \%\ \var{b}}}
322 {\code{mod(\var{a}, \var{b})}}
323 \lineiii{Multiplication}{\code{\var{a} * \var{b}}}
324 {\code{mul(\var{a}, \var{b})}}
325 \lineiii{Negation (Arithmetic)}{\code{- \var{a}}}
326 {\code{neg(\var{a})}}
327 \lineiii{Negation (Logical)}{\code{not \var{a}}}
328 {\code{not_(\var{a})}}
329 \lineiii{Right Shift}{\code{\var{a} >\code{>} \var{b}}}
330 {\code{rshift(\var{a}, \var{b})}}
331 \lineiii{Sequence Repitition}{\code{\var{seq} * \var{i}}}
332 {\code{repeat(\var{seq}, \var{i})}}
333 \lineiii{Slice Assignment}{\code{\var{seq}[\var{i}:\var{j}]} = \var{values}}
334 {\code{setslice(\var{seq}, \var{i}, \var{j}, \var{values})}}
335 \lineiii{Slice Deletion}{\code{del \var{seq}[\var{i}:\var{j}]}}
336 {\code{delslice(\var{seq}, \var{i}, \var{j})}}
337 \lineiii{Slicing}{\code{\var{seq}[\var{i}:\var{j}]}}
338 {\code{getslice(\var{seq}, \var{i}, \var{j})}}
339 \lineiii{String Formatting}{\code{\var{s} \%\ \var{o}}}
340 {\code{mod(\var{s}, \var{o})}}
341 \lineiii{Subtraction}{\code{\var{a} - \var{b}}}
342 {\code{sub(\var{a}, \var{b})}}
343 \lineiii{Truth Test}{\code{\var{o}}}
344 {\code{truth(\var{o})}}
Fred Drakee89659c2001-08-10 15:55:09 +0000345 \lineiii{Ordering}{\code{\var{a} < \var{b}}}
346 {\code{lt(\var{a}, \var{b})}}
347 \lineiii{Ordering}{\code{\var{a} <= \var{b}}}
348 {\code{le(\var{a}, \var{b})}}
349 \lineiii{Equality}{\code{\var{a} == \var{b}}}
350 {\code{eq(\var{a}, \var{b})}}
351 \lineiii{Difference}{\code{\var{a} != \var{b}}}
352 {\code{ne(\var{a}, \var{b})}}
353 \lineiii{Ordering}{\code{\var{a} >= \var{b}}}
354 {\code{ge(\var{a}, \var{b})}}
355 \lineiii{Ordering}{\code{\var{a} > \var{b}}}
356 {\code{gt(\var{a}, \var{b})}}
Fred Drake8c2fd492000-10-22 03:19:30 +0000357\end{tableiii}