blob: 69ae23b048f78fa7aa23d1c2fda965a7e1ca3711 [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 Drakec07ae9f1998-03-08 05:56:15 +000058\funcline{__inv__}{o}
Fred Drake5316ef42000-09-17 16:10:25 +000059\funcline{__invert__}{o}
60Return the inverse of \var{o}. The names \function{invert()} and
61\function{__invert__()} were added in Python 2.0.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000062\end{funcdesc}
63
Fred Drake98b032a1997-12-04 14:20:59 +000064\begin{funcdesc}{lshift}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000065\funcline{__lshift__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000066Return \var{a} shifted left by \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000067\end{funcdesc}
68
Fred Drake98b032a1997-12-04 14:20:59 +000069\begin{funcdesc}{rshift}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000070\funcline{__rshift__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000071Return \var{a} shifted right by \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000072\end{funcdesc}
73
Fred Drake98b032a1997-12-04 14:20:59 +000074\begin{funcdesc}{and_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000075\funcline{__and__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000076Return the bitwise and of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000077\end{funcdesc}
78
Fred Drake98b032a1997-12-04 14:20:59 +000079\begin{funcdesc}{or_}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +000080\funcline{__or__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +000081Return the bitwise or of \var{a} and \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +000082\end{funcdesc}
83
Guido van Rossuma58e9ed1998-05-22 18:48:37 +000084\begin{funcdesc}{xor}{a, b}
85\funcline{__xor__}{a, b}
86Return the bitwise exclusive or of \var{a} and \var{b}.
87\end{funcdesc}
88
89\begin{funcdesc}{not_}{o}
90\funcline{__not__}{o}
Fred Drakee55702b1999-06-15 20:56:40 +000091Return the outcome of \keyword{not} \var{o}. (Note that there is no
92\method{__not__()} discipline for object instances; only the
93interpreter core defines this operation.)
Guido van Rossuma58e9ed1998-05-22 18:48:37 +000094\end{funcdesc}
95
96\begin{funcdesc}{truth}{o}
Fred Drake295da241998-08-10 19:42:37 +000097Return \code{1} if \var{o} is true, and 0 otherwise.
Guido van Rossuma58e9ed1998-05-22 18:48:37 +000098\end{funcdesc}
99
Fred Drake98b032a1997-12-04 14:20:59 +0000100\begin{funcdesc}{concat}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000101\funcline{__concat__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000102Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000103\end{funcdesc}
104
Fred Drake98b032a1997-12-04 14:20:59 +0000105\begin{funcdesc}{repeat}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000106\funcline{__repeat__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000107Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
108\var{b} is an integer.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000109\end{funcdesc}
110
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000111\begin{funcdesc}{contains}{a, b}
Fred Drake5316ef42000-09-17 16:10:25 +0000112\funcline{__contains__}{a, b}
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000113Return the outcome of the test \var{b} \code{in} \var{a}.
Fred Drake5316ef42000-09-17 16:10:25 +0000114Note the reversed operands. The name \function{__contains__()} was
115added in Python 2.0.
116\end{funcdesc}
117
118\begin{funcdesc}{sequenceIncludes}{\unspecified}
119\deprecated{2.0}{Use \function{contains()} instead.}
120Alias for \function{contains()}.
Guido van Rossuma58e9ed1998-05-22 18:48:37 +0000121\end{funcdesc}
122
123\begin{funcdesc}{countOf}{a, b}
124Return the number of occurrences of \var{b} in \var{a}.
125\end{funcdesc}
126
127\begin{funcdesc}{indexOf}{a, b}
128Return the index of the first of occurrence of \var{b} in \var{a}.
129\end{funcdesc}
130
Fred Drake98b032a1997-12-04 14:20:59 +0000131\begin{funcdesc}{getitem}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000132\funcline{__getitem__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000133Return the value of \var{a} at index \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000134\end{funcdesc}
135
Fred Drake98b032a1997-12-04 14:20:59 +0000136\begin{funcdesc}{setitem}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000137\funcline{__setitem__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000138Set the value of \var{a} at index \var{b} to \var{c}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000139\end{funcdesc}
140
Fred Drake98b032a1997-12-04 14:20:59 +0000141\begin{funcdesc}{delitem}{a, b}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000142\funcline{__delitem__}{a, b}
Fred Drake0514ce11997-12-16 14:29:48 +0000143Remove the value of \var{a} at index \var{b}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000144\end{funcdesc}
145
Fred Drake98b032a1997-12-04 14:20:59 +0000146\begin{funcdesc}{getslice}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000147\funcline{__getslice__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000148Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000149\end{funcdesc}
150
Fred Drake98b032a1997-12-04 14:20:59 +0000151\begin{funcdesc}{setslice}{a, b, c, v}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000152\funcline{__setslice__}{a, b, c, v}
Fred Drake0514ce11997-12-16 14:29:48 +0000153Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
154sequence \var{v}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000155\end{funcdesc}
156
Fred Drake98b032a1997-12-04 14:20:59 +0000157\begin{funcdesc}{delslice}{a, b, c}
Fred Drakec07ae9f1998-03-08 05:56:15 +0000158\funcline{__delslice__}{a, b, c}
Fred Drake0514ce11997-12-16 14:29:48 +0000159Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000160\end{funcdesc}
161
Fred Drake8d3312f2000-10-02 03:36:18 +0000162The \module{operator} also defines a few predicates to test the type
163of objects. \strong{Note:} Be careful not to misinterpret the
164results of these functions; only \function{isCallable()} has any
165measure of reliability with instance objects. For example:
166
167\begin{verbatim}
168>>> class C:
169... pass
170...
171>>> import operator
172>>> o = C()
173>>> operator.isMappingType(o)
1741
175\end{verbatim}
176
177\begin{funcdesc}{isCallable}{o}
178\deprecated{2.0}{Use the \function{callable()} built-in function instead.}
179Returns true if the object \var{o} can be called like a function,
180otherwise it returns false. True is returned for functions, bound and
181unbound methods, class objects, and instance objects which support the
182\method{__call__()} method.
183\end{funcdesc}
184
185\begin{funcdesc}{isMappingType}{o}
186Returns true if the object \var{o} supports the mapping interface.
187This is true for dictionaries and all instance objects.
188\strong{Warning:} There is no reliable way to test if an instance
189supports the complete mapping protocol since the interface itself is
190ill-defined. This makes this test less useful than it otherwise might
191be.
192\end{funcdesc}
193
194\begin{funcdesc}{isNumberType}{o}
195Returns true if the object \var{o} represents a number. This is true
196for all numeric types implemented in C, and for all instance objects.
197\strong{Warning:} There is no reliable way to test if an instance
198supports the complete numeric interface since the interface itself is
199ill-defined. This makes this test less useful than it otherwise might
200be.
201\end{funcdesc}
202
203\begin{funcdesc}{isSequenceType}{o}
204Returns true if the object \var{o} supports the sequence protocol.
205This returns true for all objects which define sequence methods in C,
206and for all instance objects. \strong{Warning:} There is no reliable
207way to test if an instance supports the complete sequence interface
208since the interface itself is ill-defined. This makes this test less
209useful than it otherwise might be.
210\end{funcdesc}
211
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000212
Fred Drake0514ce11997-12-16 14:29:48 +0000213Example: Build a dictionary that maps the ordinals from \code{0} to
214\code{256} to their character equivalents.
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000215
Fred Drake19479911998-02-13 06:58:54 +0000216\begin{verbatim}
Guido van Rossum61ed4db1996-12-06 21:22:41 +0000217>>> import operator
218>>> d = {}
219>>> keys = range(256)
220>>> vals = map(chr, keys)
221>>> map(operator.setitem, [d]*len(keys), keys, vals)
Fred Drake19479911998-02-13 06:58:54 +0000222\end{verbatim}