cpython/Doc/liboperator.tex

133 lines
3.4 KiB
TeX
Raw Normal View History

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