Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Doc / liboperator.tex
blob2169ea89bf7b44781cdca01ab696eb5535de0375
1 % Contributed by Skip Montanaro, from the module's doc strings.
3 \section{Built-in Module \module{operator}}
4 \label{module-operator}
5 \bimodindex{operator}
7 The \module{operator} module exports a set of functions implemented in C
8 corresponding to the intrinsic operators of Python. For example,
9 \code{operator.add(x, y)} is equivalent to the expression \code{x+y}. The
10 function names are those used for special class methods; variants without
11 leading and trailing \samp{__} are also provided for convenience.
13 The \module{operator} module defines the following functions:
15 \begin{funcdesc}{add}{a, b}
16 \funcline{__add__}{a, b}
17 Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers.
18 \end{funcdesc}
20 \begin{funcdesc}{sub}{a, b}
21 \funcline{__sub__}{a, b}
22 Return \var{a} \code{-} \var{b}.
23 \end{funcdesc}
25 \begin{funcdesc}{mul}{a, b}
26 \funcline{__mul__}{a, b}
27 Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers.
28 \end{funcdesc}
30 \begin{funcdesc}{div}{a, b}
31 \funcline{__div__}{a, b}
32 Return \var{a} \code{/} \var{b}.
33 \end{funcdesc}
35 \begin{funcdesc}{mod}{a, b}
36 \funcline{__mod__}{a, b}
37 Return \var{a} \code{\%} \var{b}.
38 \end{funcdesc}
40 \begin{funcdesc}{neg}{o}
41 \funcline{__neg__}{o}
42 Return \var{o} negated.
43 \end{funcdesc}
45 \begin{funcdesc}{pos}{o}
46 \funcline{__pos__}{o}
47 Return \var{o} positive.
48 \end{funcdesc}
50 \begin{funcdesc}{abs}{o}
51 \funcline{__abs__}{o}
52 Return the absolute value of \var{o}.
53 \end{funcdesc}
55 \begin{funcdesc}{inv}{o}
56 \funcline{__inv__}{o}
57 Return the inverse of \var{o}.
58 \end{funcdesc}
60 \begin{funcdesc}{lshift}{a, b}
61 \funcline{__lshift__}{a, b}
62 Return \var{a} shifted left by \var{b}.
63 \end{funcdesc}
65 \begin{funcdesc}{rshift}{a, b}
66 \funcline{__rshift__}{a, b}
67 Return \var{a} shifted right by \var{b}.
68 \end{funcdesc}
70 \begin{funcdesc}{and_}{a, b}
71 \funcline{__and__}{a, b}
72 Return the bitwise and of \var{a} and \var{b}.
73 \end{funcdesc}
75 \begin{funcdesc}{or_}{a, b}
76 \funcline{__or__}{a, b}
77 Return the bitwise or of \var{a} and \var{b}.
78 \end{funcdesc}
80 \begin{funcdesc}{concat}{a, b}
81 \funcline{__concat__}{a, b}
82 Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
83 \end{funcdesc}
85 \begin{funcdesc}{repeat}{a, b}
86 \funcline{__repeat__}{a, b}
87 Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
88 \var{b} is an integer.
89 \end{funcdesc}
91 \begin{funcdesc}{getitem}{a, b}
92 \funcline{__getitem__}{a, b}
93 Return the value of \var{a} at index \var{b}.
94 \end{funcdesc}
96 \begin{funcdesc}{setitem}{a, b, c}
97 \funcline{__setitem__}{a, b, c}
98 Set the value of \var{a} at index \var{b} to \var{c}.
99 \end{funcdesc}
101 \begin{funcdesc}{delitem}{a, b}
102 \funcline{__delitem__}{a, b}
103 Remove the value of \var{a} at index \var{b}.
104 \end{funcdesc}
106 \begin{funcdesc}{getslice}{a, b, c}
107 \funcline{__getslice__}{a, b, c}
108 Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
109 \end{funcdesc}
111 \begin{funcdesc}{setslice}{a, b, c, v}
112 \funcline{__setslice__}{a, b, c, v}
113 Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
114 sequence \var{v}.
115 \end{funcdesc}
117 \begin{funcdesc}{delslice}{a, b, c}
118 \funcline{__delslice__}{a, b, c}
119 Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
120 \end{funcdesc}
123 Example: Build a dictionary that maps the ordinals from \code{0} to
124 \code{256} to their character equivalents.
126 \begin{verbatim}
127 >>> import operator
128 >>> d = {}
129 >>> keys = range(256)
130 >>> vals = map(chr, keys)
131 >>> map(operator.setitem, [d]*len(keys), keys, vals)
132 \end{verbatim}