blob: 14c48a4814a365ff378522693a4d8c6aa2fe1406 [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 Drakec07ae9f1998-03-08 05:56:15 +000015The \module{operator} module defines the following functions:
Guido van Rossum61ed4db1996-12-06 21:22:41 +000016
Fred Drake98b032a1997-12-04 14:20:59 +000017\begin{funcdesc}{add}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000018\funcline{__add__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000019Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000020\end{funcdesc}
21
Fred Drake98b032a1997-12-04 14:20:59 +000022\begin{funcdesc}{sub}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000023\funcline{__sub__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000024Return \var{a} \code{-} \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000025\end{funcdesc}
26
Fred Drake98b032a1997-12-04 14:20:59 +000027\begin{funcdesc}{mul}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000028\funcline{__mul__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000029Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000030\end{funcdesc}
31
Fred Drake98b032a1997-12-04 14:20:59 +000032\begin{funcdesc}{div}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000033\funcline{__div__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000034Return \var{a} \code{/} \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000035\end{funcdesc}
36
Fred Drake98b032a1997-12-04 14:20:59 +000037\begin{funcdesc}{mod}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000038\funcline{__mod__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000039Return \var{a} \code{\%} \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000040\end{funcdesc}
41
42\begin{funcdesc}{neg}{o}
Fred Drakec07ae9f1998-03-08 05:56:15 +000043\funcline{__neg__}{o}
Fred Drake0514ce11997-12-16 14:29:48 +000044Return \var{o} negated.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000045\end{funcdesc}
46
47\begin{funcdesc}{pos}{o}
Fred Drakec07ae9f1998-03-08 05:56:15 +000048\funcline{__pos__}{o}
Fred Drake0514ce11997-12-16 14:29:48 +000049Return \var{o} positive.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000050\end{funcdesc}
51
52\begin{funcdesc}{abs}{o}
Fred Drakec07ae9f1998-03-08 05:56:15 +000053\funcline{__abs__}{o}
Fred Drake0514ce11997-12-16 14:29:48 +000054Return the absolute value of \var{o}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000055\end{funcdesc}
56
57\begin{funcdesc}{inv}{o}
Fred Drake8c2fd492000-10-22 03:19:30 +000058\funcline{invert}{o}
Fred Drakec07ae9f1998-03-08 05:56:15 +000059\funcline{__inv__}{o}
Fred Drake5316ef42000-09-17 16:10:25 +000060\funcline{__invert__}{o}
61Return the inverse of \var{o}. The names \function{invert()} and
62\function{__invert__()} were added in Python 2.0.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000063\end{funcdesc}
64
Fred Drake98b032a1997-12-04 14:20:59 +000065\begin{funcdesc}{lshift}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000066\funcline{__lshift__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000067Return \var{a} shifted left by \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000068\end{funcdesc}
69
Fred Drake98b032a1997-12-04 14:20:59 +000070\begin{funcdesc}{rshift}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000071\funcline{__rshift__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000072Return \var{a} shifted right by \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000073\end{funcdesc}
74
Fred Drake98b032a1997-12-04 14:20:59 +000075\begin{funcdesc}{and_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000076\funcline{__and__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000077Return the bitwise and of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000078\end{funcdesc}
79
Fred Drake98b032a1997-12-04 14:20:59 +000080\begin{funcdesc}{or_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000081\funcline{__or__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000082Return the bitwise or of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000083\end{funcdesc}
84
Guido van Rossuma58e9ed1998-05-22 18:48:37 +000085\begin{funcdesc}{xor}{a, b}
86\funcline{__xor__}{a, b}
87Return the bitwise exclusive or of \var{a} and \var{b}.
88\end{funcdesc}
89
90\begin{funcdesc}{not_}{o}
91\funcline{__not__}{o}
Fred Drakee55702b1999-06-15 20:56:40 +000092Return the outcome of \keyword{not} \var{o}. (Note that there is no
Fred Draked88d0a12000-10-06 19:39:47 +000093\method{__not__()} method for object instances; only the interpreter
94core defines this operation.)
Guido van Rossuma58e9ed1998-05-22 18:48:37 +000095\end{funcdesc}
96
97\begin{funcdesc}{truth}{o}
Fred Drake295da241998-08-10 19:42:37 +000098Return \code{1} if \var{o} is true, and 0 otherwise.
Guido van Rossuma58e9ed1998-05-22 18:48:37 +000099\end{funcdesc}
100
Fred Drake98b032a1997-12-04 14:20:59 +0000101\begin{funcdesc}{concat}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000102\funcline{__concat__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000103Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000104\end{funcdesc}
105
Fred Drake98b032a1997-12-04 14:20:59 +0000106\begin{funcdesc}{repeat}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000107\funcline{__repeat__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000108Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
109\var{b} is an integer.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000110\end{funcdesc}
111
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000112\begin{funcdesc}{contains}{a, b}
Fred Drake5316ef42000-09-17 16:10:25 +0000113\funcline{__contains__}{a, b}
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000114Return the outcome of the test \var{b} \code{in} \var{a}.
Fred Drake5316ef42000-09-17 16:10:25 +0000115Note the reversed operands. The name \function{__contains__()} was
116added in Python 2.0.
117\end{funcdesc}
118
119\begin{funcdesc}{sequenceIncludes}{\unspecified}
120\deprecated{2.0}{Use \function{contains()} instead.}
121Alias for \function{contains()}.
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000122\end{funcdesc}
123
124\begin{funcdesc}{countOf}{a, b}
125Return the number of occurrences of \var{b} in \var{a}.
126\end{funcdesc}
127
128\begin{funcdesc}{indexOf}{a, b}
129Return the index of the first of occurrence of \var{b} in \var{a}.
130\end{funcdesc}
131
Fred Drake98b032a1997-12-04 14:20:59 +0000132\begin{funcdesc}{getitem}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000133\funcline{__getitem__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000134Return the value of \var{a} at index \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000135\end{funcdesc}
136
Fred Drake98b032a1997-12-04 14:20:59 +0000137\begin{funcdesc}{setitem}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000138\funcline{__setitem__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000139Set the value of \var{a} at index \var{b} to \var{c}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000140\end{funcdesc}
141
Fred Drake98b032a1997-12-04 14:20:59 +0000142\begin{funcdesc}{delitem}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000143\funcline{__delitem__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000144Remove the value of \var{a} at index \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000145\end{funcdesc}
146
Fred Drake98b032a1997-12-04 14:20:59 +0000147\begin{funcdesc}{getslice}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000148\funcline{__getslice__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000149Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000150\end{funcdesc}
151
Fred Drake98b032a1997-12-04 14:20:59 +0000152\begin{funcdesc}{setslice}{a, b, c, v}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000153\funcline{__setslice__}{a, b, c, v}
Fred Drake0514ce11997-12-16 14:29:48 +0000154Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
155sequence \var{v}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000156\end{funcdesc}
157
Fred Drake98b032a1997-12-04 14:20:59 +0000158\begin{funcdesc}{delslice}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000159\funcline{__delslice__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000160Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000161\end{funcdesc}
162
Fred Drake8d3312f2000-10-02 03:36:18 +0000163The \module{operator} also defines a few predicates to test the type
164of objects. \strong{Note:} Be careful not to misinterpret the
165results of these functions; only \function{isCallable()} has any
166measure of reliability with instance objects. For example:
167
168\begin{verbatim}
169>>> class C:
170... pass
171...
172>>> import operator
173>>> o = C()
174>>> operator.isMappingType(o)
1751
176\end{verbatim}
177
178\begin{funcdesc}{isCallable}{o}
179\deprecated{2.0}{Use the \function{callable()} built-in function instead.}
180Returns true if the object \var{o} can be called like a function,
181otherwise it returns false. True is returned for functions, bound and
182unbound methods, class objects, and instance objects which support the
183\method{__call__()} method.
184\end{funcdesc}
185
186\begin{funcdesc}{isMappingType}{o}
187Returns true if the object \var{o} supports the mapping interface.
188This is true for dictionaries and all instance objects.
189\strong{Warning:} There is no reliable way to test if an instance
190supports the complete mapping protocol since the interface itself is
191ill-defined. This makes this test less useful than it otherwise might
192be.
193\end{funcdesc}
194
195\begin{funcdesc}{isNumberType}{o}
196Returns true if the object \var{o} represents a number. This is true
197for all numeric types implemented in C, and for all instance objects.
198\strong{Warning:} There is no reliable way to test if an instance
199supports the complete numeric interface since the interface itself is
200ill-defined. This makes this test less useful than it otherwise might
201be.
202\end{funcdesc}
203
204\begin{funcdesc}{isSequenceType}{o}
205Returns true if the object \var{o} supports the sequence protocol.
206This returns true for all objects which define sequence methods in C,
207and for all instance objects. \strong{Warning:} There is no reliable
208way to test if an instance supports the complete sequence interface
209since the interface itself is ill-defined. This makes this test less
210useful than it otherwise might be.
211\end{funcdesc}
212
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000213
Fred Drake0514ce11997-12-16 14:29:48 +0000214Example: Build a dictionary that maps the ordinals from \code{0} to
215\code{256} to their character equivalents.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000216
Fred Drake19479911998-02-13 06:58:54 +0000217\begin{verbatim}
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000218>>> import operator
219>>> d = {}
220>>> keys = range(256)
221>>> vals = map(chr, keys)
222>>> map(operator.setitem, [d]*len(keys), keys, vals)
Fred Drake19479911998-02-13 06:58:54 +0000223\end{verbatim}
Fred Drake8c2fd492000-10-22 03:19:30 +0000224
225
226\subsection{Mapping Operators to Functions \label{operator-map}}
227
228This table shows how abstract operations correspond to operator
229symbols in the Python syntax and the functions in the
230\refmodule{operator} module.
231
232
233\begin{tableiii}{l|c|l}{textrm}{Operation}{Syntax}{Function}
234 \lineiii{Addition}{\code{\var{a} + \var{b}}}
235 {\code{add(\var{a}, \var{b})}}
236 \lineiii{Concatenation}{\code{\var{seq1} + \var{seq2}}}
237 {\code{concat(\var{seq1}, \var{seq2})}}
238 \lineiii{Containment Test}{\code{\var{o} in \var{seq}}}
239 {\code{contains(\var{seq}, \var{o})}}
240 \lineiii{Division}{\code{\var{a} / \var{b}}}
241 {\code{div(\var{a}, \var{b})}}
242 \lineiii{Bitwise And}{\code{\var{a} \&\ \var{b}}}
243 {\code{and_(\var{a}, \var{b})}}
244 \lineiii{Bitwise Exclusive Or}{\code{\var{a} \^\ \var{b}}}
245 {\code{xor(\var{a}, \var{b})}}
246 \lineiii{Bitwise Inversion}{\code{\~{} \var{a}}}
247 {\code{invert(\var{a})}}
248 \lineiii{Bitwise Or}{\code{\var{a} | \var{b}}}
249 {\code{or_(\var{a}, \var{b})}}
250 \lineiii{Indexed Assignment}{\code{\var{o}[\var{k}] = \var{v}}}
251 {\code{setitem(\var{o}, \var{k}, \var{v})}}
252 \lineiii{Indexed Deletion}{\code{del \var{o}[\var{k}]}}
253 {\code{delitem(\var{o}, \var{k})}}
254 \lineiii{Indexing}{\code{\var{o}[\var{k}]}}
255 {\code{getitem(\var{o}, \var{k})}}
256 \lineiii{Left Shift}{\code{\var{a} <\code{<} \var{b}}}
257 {\code{lshift(\var{a}, \var{b})}}
258 \lineiii{Modulo}{\code{\var{a} \%\ \var{b}}}
259 {\code{mod(\var{a}, \var{b})}}
260 \lineiii{Multiplication}{\code{\var{a} * \var{b}}}
261 {\code{mul(\var{a}, \var{b})}}
262 \lineiii{Negation (Arithmetic)}{\code{- \var{a}}}
263 {\code{neg(\var{a})}}
264 \lineiii{Negation (Logical)}{\code{not \var{a}}}
265 {\code{not_(\var{a})}}
266 \lineiii{Right Shift}{\code{\var{a} >\code{>} \var{b}}}
267 {\code{rshift(\var{a}, \var{b})}}
268 \lineiii{Sequence Repitition}{\code{\var{seq} * \var{i}}}
269 {\code{repeat(\var{seq}, \var{i})}}
270 \lineiii{Slice Assignment}{\code{\var{seq}[\var{i}:\var{j}]} = \var{values}}
271 {\code{setslice(\var{seq}, \var{i}, \var{j}, \var{values})}}
272 \lineiii{Slice Deletion}{\code{del \var{seq}[\var{i}:\var{j}]}}
273 {\code{delslice(\var{seq}, \var{i}, \var{j})}}
274 \lineiii{Slicing}{\code{\var{seq}[\var{i}:\var{j}]}}
275 {\code{getslice(\var{seq}, \var{i}, \var{j})}}
276 \lineiii{String Formatting}{\code{\var{s} \%\ \var{o}}}
277 {\code{mod(\var{s}, \var{o})}}
278 \lineiii{Subtraction}{\code{\var{a} - \var{b}}}
279 {\code{sub(\var{a}, \var{b})}}
280 \lineiii{Truth Test}{\code{\var{o}}}
281 {\code{truth(\var{o})}}
282\end{tableiii}