blob: 9c7c3892ed263846a27f388941d2559f4126f075 [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}
Fred Drake29c45a52000-12-15 05:41:49 +000061Return the bitwise inverse of the number \var{o}. The names
62\function{invert()} and \function{__invert__()} were added in Python
632.0.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000064\end{funcdesc}
65
Fred Drake98b032a1997-12-04 14:20:59 +000066\begin{funcdesc}{lshift}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000067\funcline{__lshift__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000068Return \var{a} shifted left by \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000069\end{funcdesc}
70
Fred Drake98b032a1997-12-04 14:20:59 +000071\begin{funcdesc}{rshift}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000072\funcline{__rshift__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000073Return \var{a} shifted right by \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000074\end{funcdesc}
75
Fred Drake98b032a1997-12-04 14:20:59 +000076\begin{funcdesc}{and_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000077\funcline{__and__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000078Return the bitwise and of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000079\end{funcdesc}
80
Fred Drake98b032a1997-12-04 14:20:59 +000081\begin{funcdesc}{or_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000082\funcline{__or__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000083Return the bitwise or of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000084\end{funcdesc}
85
Guido van Rossuma58e9ed1998-05-22 18:48:37 +000086\begin{funcdesc}{xor}{a, b}
87\funcline{__xor__}{a, b}
88Return the bitwise exclusive or of \var{a} and \var{b}.
89\end{funcdesc}
90
91\begin{funcdesc}{not_}{o}
92\funcline{__not__}{o}
Fred Drakee55702b1999-06-15 20:56:40 +000093Return the outcome of \keyword{not} \var{o}. (Note that there is no
Fred Draked88d0a12000-10-06 19:39:47 +000094\method{__not__()} method for object instances; only the interpreter
95core defines this operation.)
Guido van Rossuma58e9ed1998-05-22 18:48:37 +000096\end{funcdesc}
97
98\begin{funcdesc}{truth}{o}
Fred Drake295da241998-08-10 19:42:37 +000099Return \code{1} if \var{o} is true, and 0 otherwise.
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000100\end{funcdesc}
101
Fred Drake98b032a1997-12-04 14:20:59 +0000102\begin{funcdesc}{concat}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000103\funcline{__concat__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000104Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000105\end{funcdesc}
106
Fred Drake98b032a1997-12-04 14:20:59 +0000107\begin{funcdesc}{repeat}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000108\funcline{__repeat__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000109Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
110\var{b} is an integer.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000111\end{funcdesc}
112
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000113\begin{funcdesc}{contains}{a, b}
Fred Drake5316ef42000-09-17 16:10:25 +0000114\funcline{__contains__}{a, b}
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000115Return the outcome of the test \var{b} \code{in} \var{a}.
Fred Drake5316ef42000-09-17 16:10:25 +0000116Note the reversed operands. The name \function{__contains__()} was
117added in Python 2.0.
118\end{funcdesc}
119
120\begin{funcdesc}{sequenceIncludes}{\unspecified}
121\deprecated{2.0}{Use \function{contains()} instead.}
122Alias for \function{contains()}.
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000123\end{funcdesc}
124
125\begin{funcdesc}{countOf}{a, b}
126Return the number of occurrences of \var{b} in \var{a}.
127\end{funcdesc}
128
129\begin{funcdesc}{indexOf}{a, b}
130Return the index of the first of occurrence of \var{b} in \var{a}.
131\end{funcdesc}
132
Fred Drake98b032a1997-12-04 14:20:59 +0000133\begin{funcdesc}{getitem}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000134\funcline{__getitem__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000135Return the value of \var{a} at index \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000136\end{funcdesc}
137
Fred Drake98b032a1997-12-04 14:20:59 +0000138\begin{funcdesc}{setitem}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000139\funcline{__setitem__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000140Set the value of \var{a} at index \var{b} to \var{c}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000141\end{funcdesc}
142
Fred Drake98b032a1997-12-04 14:20:59 +0000143\begin{funcdesc}{delitem}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000144\funcline{__delitem__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000145Remove the value of \var{a} at index \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000146\end{funcdesc}
147
Fred Drake98b032a1997-12-04 14:20:59 +0000148\begin{funcdesc}{getslice}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000149\funcline{__getslice__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000150Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000151\end{funcdesc}
152
Fred Drake98b032a1997-12-04 14:20:59 +0000153\begin{funcdesc}{setslice}{a, b, c, v}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000154\funcline{__setslice__}{a, b, c, v}
Fred Drake0514ce11997-12-16 14:29:48 +0000155Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
156sequence \var{v}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000157\end{funcdesc}
158
Fred Drake98b032a1997-12-04 14:20:59 +0000159\begin{funcdesc}{delslice}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000160\funcline{__delslice__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000161Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000162\end{funcdesc}
163
Fred Drake8d3312f2000-10-02 03:36:18 +0000164The \module{operator} also defines a few predicates to test the type
165of objects. \strong{Note:} Be careful not to misinterpret the
166results of these functions; only \function{isCallable()} has any
167measure of reliability with instance objects. For example:
168
169\begin{verbatim}
170>>> class C:
171... pass
172...
173>>> import operator
174>>> o = C()
175>>> operator.isMappingType(o)
1761
177\end{verbatim}
178
179\begin{funcdesc}{isCallable}{o}
180\deprecated{2.0}{Use the \function{callable()} built-in function instead.}
181Returns true if the object \var{o} can be called like a function,
182otherwise it returns false. True is returned for functions, bound and
183unbound methods, class objects, and instance objects which support the
184\method{__call__()} method.
185\end{funcdesc}
186
187\begin{funcdesc}{isMappingType}{o}
188Returns true if the object \var{o} supports the mapping interface.
189This is true for dictionaries and all instance objects.
190\strong{Warning:} There is no reliable way to test if an instance
191supports the complete mapping protocol since the interface itself is
192ill-defined. This makes this test less useful than it otherwise might
193be.
194\end{funcdesc}
195
196\begin{funcdesc}{isNumberType}{o}
197Returns true if the object \var{o} represents a number. This is true
198for all numeric types implemented in C, and for all instance objects.
199\strong{Warning:} There is no reliable way to test if an instance
200supports the complete numeric interface since the interface itself is
201ill-defined. This makes this test less useful than it otherwise might
202be.
203\end{funcdesc}
204
205\begin{funcdesc}{isSequenceType}{o}
206Returns true if the object \var{o} supports the sequence protocol.
207This returns true for all objects which define sequence methods in C,
208and for all instance objects. \strong{Warning:} There is no reliable
209way to test if an instance supports the complete sequence interface
210since the interface itself is ill-defined. This makes this test less
211useful than it otherwise might be.
212\end{funcdesc}
213
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000214
Fred Drake0514ce11997-12-16 14:29:48 +0000215Example: Build a dictionary that maps the ordinals from \code{0} to
216\code{256} to their character equivalents.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000217
Fred Drake19479911998-02-13 06:58:54 +0000218\begin{verbatim}
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000219>>> import operator
220>>> d = {}
221>>> keys = range(256)
222>>> vals = map(chr, keys)
223>>> map(operator.setitem, [d]*len(keys), keys, vals)
Fred Drake19479911998-02-13 06:58:54 +0000224\end{verbatim}
Fred Drake8c2fd492000-10-22 03:19:30 +0000225
226
227\subsection{Mapping Operators to Functions \label{operator-map}}
228
229This table shows how abstract operations correspond to operator
230symbols in the Python syntax and the functions in the
231\refmodule{operator} module.
232
233
234\begin{tableiii}{l|c|l}{textrm}{Operation}{Syntax}{Function}
235 \lineiii{Addition}{\code{\var{a} + \var{b}}}
236 {\code{add(\var{a}, \var{b})}}
237 \lineiii{Concatenation}{\code{\var{seq1} + \var{seq2}}}
238 {\code{concat(\var{seq1}, \var{seq2})}}
239 \lineiii{Containment Test}{\code{\var{o} in \var{seq}}}
240 {\code{contains(\var{seq}, \var{o})}}
241 \lineiii{Division}{\code{\var{a} / \var{b}}}
242 {\code{div(\var{a}, \var{b})}}
243 \lineiii{Bitwise And}{\code{\var{a} \&\ \var{b}}}
244 {\code{and_(\var{a}, \var{b})}}
245 \lineiii{Bitwise Exclusive Or}{\code{\var{a} \^\ \var{b}}}
246 {\code{xor(\var{a}, \var{b})}}
247 \lineiii{Bitwise Inversion}{\code{\~{} \var{a}}}
248 {\code{invert(\var{a})}}
249 \lineiii{Bitwise Or}{\code{\var{a} | \var{b}}}
250 {\code{or_(\var{a}, \var{b})}}
251 \lineiii{Indexed Assignment}{\code{\var{o}[\var{k}] = \var{v}}}
252 {\code{setitem(\var{o}, \var{k}, \var{v})}}
253 \lineiii{Indexed Deletion}{\code{del \var{o}[\var{k}]}}
254 {\code{delitem(\var{o}, \var{k})}}
255 \lineiii{Indexing}{\code{\var{o}[\var{k}]}}
256 {\code{getitem(\var{o}, \var{k})}}
257 \lineiii{Left Shift}{\code{\var{a} <\code{<} \var{b}}}
258 {\code{lshift(\var{a}, \var{b})}}
259 \lineiii{Modulo}{\code{\var{a} \%\ \var{b}}}
260 {\code{mod(\var{a}, \var{b})}}
261 \lineiii{Multiplication}{\code{\var{a} * \var{b}}}
262 {\code{mul(\var{a}, \var{b})}}
263 \lineiii{Negation (Arithmetic)}{\code{- \var{a}}}
264 {\code{neg(\var{a})}}
265 \lineiii{Negation (Logical)}{\code{not \var{a}}}
266 {\code{not_(\var{a})}}
267 \lineiii{Right Shift}{\code{\var{a} >\code{>} \var{b}}}
268 {\code{rshift(\var{a}, \var{b})}}
269 \lineiii{Sequence Repitition}{\code{\var{seq} * \var{i}}}
270 {\code{repeat(\var{seq}, \var{i})}}
271 \lineiii{Slice Assignment}{\code{\var{seq}[\var{i}:\var{j}]} = \var{values}}
272 {\code{setslice(\var{seq}, \var{i}, \var{j}, \var{values})}}
273 \lineiii{Slice Deletion}{\code{del \var{seq}[\var{i}:\var{j}]}}
274 {\code{delslice(\var{seq}, \var{i}, \var{j})}}
275 \lineiii{Slicing}{\code{\var{seq}[\var{i}:\var{j}]}}
276 {\code{getslice(\var{seq}, \var{i}, \var{j})}}
277 \lineiii{String Formatting}{\code{\var{s} \%\ \var{o}}}
278 {\code{mod(\var{s}, \var{o})}}
279 \lineiii{Subtraction}{\code{\var{a} - \var{b}}}
280 {\code{sub(\var{a}, \var{b})}}
281 \lineiii{Truth Test}{\code{\var{o}}}
282 {\code{truth(\var{o})}}
283\end{tableiii}