1996-12-06 17:22:41 -04:00
|
|
|
% Contributed by Skip Montanaro, from the module's doc strings.
|
|
|
|
|
1997-12-16 10:29:48 -04:00
|
|
|
\section{Built-in Module \sectcode{operator}}
|
1997-12-15 20:02:01 -04:00
|
|
|
\bimodindex{operator}
|
1996-12-06 17:22:41 -04:00
|
|
|
|
|
|
|
The \code{operator} module exports a set of functions implemented in C
|
|
|
|
corresponding to the intrinsic operators of Python. For example,
|
1997-12-16 10:29:48 -04:00
|
|
|
{}\code{operator.add(x, y)} is equivalent to the expression \code{x+y}. The
|
1996-12-06 17:22:41 -04:00
|
|
|
function names are those used for special class methods; variants without
|
1997-12-04 10:20:59 -04:00
|
|
|
leading and trailing \samp{__} are also provided for convenience.
|
1996-12-06 17:22:41 -04:00
|
|
|
|
|
|
|
The \code{operator} module defines the following functions:
|
|
|
|
|
|
|
|
\renewcommand{\indexsubitem}{(in module operator)}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{add}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__add__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{sub}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{-} \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__sub__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{-} \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{mul}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__mul__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{div}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{/} \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__div__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{/} \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{mod}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{\%} \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__mod__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{\%} \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{neg}{o}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{o} negated.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__neg__}{o}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{o} negated.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{pos}{o}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{o} positive.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__pos__}{o}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{o} positive.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{abs}{o}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the absolute value of \var{o}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__abs__}{o}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the absolute value of \var{o}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{inv}{o}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the inverse of \var{o}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__inv__}{o}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the inverse of \var{o}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{lshift}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} shifted left by \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__lshift__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} shifted left by \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{rshift}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} shifted right by \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__rshift__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} shifted right by \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{and_}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the bitwise and of \var{a} and \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__and__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the bitwise and of \var{a} and \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{or_}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the bitwise or of \var{a} and \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__or__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the bitwise or of \var{a} and \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{concat}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__concat__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{repeat}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
|
|
|
|
\var{b} is an integer.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__repeat__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
|
|
|
|
\var{b} is an integer.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{getitem}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the value of \var{a} at index \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__getitem__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the value of \var{a} at index \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{setitem}{a, b, c}
|
1997-12-16 10:29:48 -04:00
|
|
|
Set the value of \var{a} at index \var{b} to \var{c}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__setitem__}{a, b, c}
|
1997-12-16 10:29:48 -04:00
|
|
|
Set the value of \var{a} at index \var{b} to \var{c}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{delitem}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Remove the value of \var{a} at index \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__delitem__}{a, b}
|
1997-12-16 10:29:48 -04:00
|
|
|
Remove the value of \var{a} at index \var{b}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{getslice}{a, b, c}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__getslice__}{a, b, c}
|
1997-12-16 10:29:48 -04:00
|
|
|
Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{setslice}{a, b, c, v}
|
1997-12-16 10:29:48 -04: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 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__setslice__}{a, b, c, v}
|
1997-12-16 10:29:48 -04: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 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{delslice}{a, b, c}
|
1997-12-16 10:29:48 -04:00
|
|
|
Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-04 10:20:59 -04:00
|
|
|
\begin{funcdesc}{__delslice__}{a, b, c}
|
1997-12-16 10:29:48 -04:00
|
|
|
Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
|
1996-12-06 17:22:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-16 10:29:48 -04:00
|
|
|
Example: Build a dictionary that maps the ordinals from \code{0} to
|
|
|
|
\code{256} to their character equivalents.
|
1996-12-06 17:22:41 -04:00
|
|
|
|
1997-07-17 13:34:52 -03:00
|
|
|
\bcode\begin{verbatim}
|
1996-12-06 17:22:41 -04:00
|
|
|
>>> import operator
|
|
|
|
>>> d = {}
|
|
|
|
>>> keys = range(256)
|
|
|
|
>>> vals = map(chr, keys)
|
|
|
|
>>> map(operator.setitem, [d]*len(keys), keys, vals)
|
1997-07-17 13:34:52 -03:00
|
|
|
\end{verbatim}\ecode
|