1998-08-10 19:42:37 +00:00
|
|
|
\section{\module{operator} ---
|
|
|
|
Standard operators as functions.}
|
1998-07-23 17:59:49 +00:00
|
|
|
\declaremodule{builtin}{operator}
|
1998-08-10 19:42:37 +00:00
|
|
|
\sectionauthor{Skip Montanaro}{skip@automatrix.com}
|
1998-07-23 17:59:49 +00:00
|
|
|
|
|
|
|
\modulesynopsis{All Python's standard operators as built-in functions.}
|
|
|
|
|
1996-12-06 21:22:41 +00:00
|
|
|
|
1998-03-08 05:56:15 +00:00
|
|
|
The \module{operator} module exports a set of functions implemented in C
|
1996-12-06 21:22:41 +00:00
|
|
|
corresponding to the intrinsic operators of Python. For example,
|
1997-12-29 17:11:55 +00:00
|
|
|
\code{operator.add(x, y)} is equivalent to the expression \code{x+y}. The
|
1996-12-06 21:22:41 +00:00
|
|
|
function names are those used for special class methods; variants without
|
1997-12-04 14:20:59 +00:00
|
|
|
leading and trailing \samp{__} are also provided for convenience.
|
1996-12-06 21:22:41 +00:00
|
|
|
|
1998-03-08 05:56:15 +00:00
|
|
|
The \module{operator} module defines the following functions:
|
1996-12-06 21:22:41 +00:00
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{add}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__add__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{sub}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__sub__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{a} \code{-} \var{b}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{mul}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__mul__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{div}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__div__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{a} \code{/} \var{b}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{mod}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__mod__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{a} \code{\%} \var{b}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{neg}{o}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__neg__}{o}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{o} negated.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{pos}{o}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__pos__}{o}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{o} positive.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{abs}{o}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__abs__}{o}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return the absolute value of \var{o}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{inv}{o}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__inv__}{o}
|
2000-09-17 16:10:25 +00:00
|
|
|
\funcline{__invert__}{o}
|
|
|
|
Return the inverse of \var{o}. The names \function{invert()} and
|
|
|
|
\function{__invert__()} were added in Python 2.0.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{lshift}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__lshift__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{a} shifted left by \var{b}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{rshift}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__rshift__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{a} shifted right by \var{b}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{and_}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__and__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return the bitwise and of \var{a} and \var{b}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{or_}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__or__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return the bitwise or of \var{a} and \var{b}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-05-22 18:48:37 +00:00
|
|
|
\begin{funcdesc}{xor}{a, b}
|
|
|
|
\funcline{__xor__}{a, b}
|
|
|
|
Return the bitwise exclusive or of \var{a} and \var{b}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{not_}{o}
|
|
|
|
\funcline{__not__}{o}
|
1999-06-15 20:56:40 +00:00
|
|
|
Return the outcome of \keyword{not} \var{o}. (Note that there is no
|
|
|
|
\method{__not__()} discipline for object instances; only the
|
|
|
|
interpreter core defines this operation.)
|
1998-05-22 18:48:37 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{truth}{o}
|
1998-08-10 19:42:37 +00:00
|
|
|
Return \code{1} if \var{o} is true, and 0 otherwise.
|
1998-05-22 18:48:37 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{concat}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__concat__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{repeat}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__repeat__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
|
|
|
|
\var{b} is an integer.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-05-22 18:48:37 +00:00
|
|
|
\begin{funcdesc}{contains}{a, b}
|
2000-09-17 16:10:25 +00:00
|
|
|
\funcline{__contains__}{a, b}
|
1998-05-22 18:48:37 +00:00
|
|
|
Return the outcome of the test \var{b} \code{in} \var{a}.
|
2000-09-17 16:10:25 +00:00
|
|
|
Note the reversed operands. The name \function{__contains__()} was
|
|
|
|
added in Python 2.0.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{sequenceIncludes}{\unspecified}
|
|
|
|
\deprecated{2.0}{Use \function{contains()} instead.}
|
|
|
|
Alias for \function{contains()}.
|
1998-05-22 18:48:37 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{countOf}{a, b}
|
|
|
|
Return the number of occurrences of \var{b} in \var{a}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{indexOf}{a, b}
|
|
|
|
Return the index of the first of occurrence of \var{b} in \var{a}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{getitem}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__getitem__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return the value of \var{a} at index \var{b}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{setitem}{a, b, c}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__setitem__}{a, b, c}
|
1997-12-16 14:29:48 +00:00
|
|
|
Set the value of \var{a} at index \var{b} to \var{c}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{delitem}{a, b}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__delitem__}{a, b}
|
1997-12-16 14:29:48 +00:00
|
|
|
Remove the value of \var{a} at index \var{b}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{getslice}{a, b, c}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__getslice__}{a, b, c}
|
1997-12-16 14:29:48 +00:00
|
|
|
Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{setslice}{a, b, c, v}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__setslice__}{a, b, c, v}
|
1997-12-16 14:29:48 +00:00
|
|
|
Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
|
|
|
|
sequence \var{v}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 14:20:59 +00:00
|
|
|
\begin{funcdesc}{delslice}{a, b, c}
|
1998-03-08 05:56:15 +00:00
|
|
|
\funcline{__delslice__}{a, b, c}
|
1997-12-16 14:29:48 +00:00
|
|
|
Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
|
1996-12-06 21:22:41 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2000-10-02 03:36:18 +00:00
|
|
|
The \module{operator} also defines a few predicates to test the type
|
|
|
|
of objects. \strong{Note:} Be careful not to misinterpret the
|
|
|
|
results of these functions; only \function{isCallable()} has any
|
|
|
|
measure of reliability with instance objects. For example:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
>>> class C:
|
|
|
|
... pass
|
|
|
|
...
|
|
|
|
>>> import operator
|
|
|
|
>>> o = C()
|
|
|
|
>>> operator.isMappingType(o)
|
|
|
|
1
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
\begin{funcdesc}{isCallable}{o}
|
|
|
|
\deprecated{2.0}{Use the \function{callable()} built-in function instead.}
|
|
|
|
Returns true if the object \var{o} can be called like a function,
|
|
|
|
otherwise it returns false. True is returned for functions, bound and
|
|
|
|
unbound methods, class objects, and instance objects which support the
|
|
|
|
\method{__call__()} method.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{isMappingType}{o}
|
|
|
|
Returns true if the object \var{o} supports the mapping interface.
|
|
|
|
This is true for dictionaries and all instance objects.
|
|
|
|
\strong{Warning:} There is no reliable way to test if an instance
|
|
|
|
supports the complete mapping protocol since the interface itself is
|
|
|
|
ill-defined. This makes this test less useful than it otherwise might
|
|
|
|
be.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{isNumberType}{o}
|
|
|
|
Returns true if the object \var{o} represents a number. This is true
|
|
|
|
for all numeric types implemented in C, and for all instance objects.
|
|
|
|
\strong{Warning:} There is no reliable way to test if an instance
|
|
|
|
supports the complete numeric interface since the interface itself is
|
|
|
|
ill-defined. This makes this test less useful than it otherwise might
|
|
|
|
be.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{isSequenceType}{o}
|
|
|
|
Returns true if the object \var{o} supports the sequence protocol.
|
|
|
|
This returns true for all objects which define sequence methods in C,
|
|
|
|
and for all instance objects. \strong{Warning:} There is no reliable
|
|
|
|
way to test if an instance supports the complete sequence interface
|
|
|
|
since the interface itself is ill-defined. This makes this test less
|
|
|
|
useful than it otherwise might be.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1996-12-06 21:22:41 +00:00
|
|
|
|
1997-12-16 14:29:48 +00:00
|
|
|
Example: Build a dictionary that maps the ordinals from \code{0} to
|
|
|
|
\code{256} to their character equivalents.
|
1996-12-06 21:22:41 +00:00
|
|
|
|
1998-02-13 06:58:54 +00:00
|
|
|
\begin{verbatim}
|
1996-12-06 21:22:41 +00:00
|
|
|
>>> import operator
|
|
|
|
>>> d = {}
|
|
|
|
>>> keys = range(256)
|
|
|
|
>>> vals = map(chr, keys)
|
|
|
|
>>> map(operator.setitem, [d]*len(keys), keys, vals)
|
1998-02-13 06:58:54 +00:00
|
|
|
\end{verbatim}
|