Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Doc / lib / liboperator.tex
blob608f1015dffaefd476262f53d47b54d28051e13d
1 \section{\module{operator} ---
2 Standard operators as functions.}
3 \declaremodule{builtin}{operator}
4 \sectionauthor{Skip Montanaro}{skip@automatrix.com}
6 \modulesynopsis{All Python's standard operators as built-in functions.}
9 The \module{operator} module exports a set of functions implemented in C
10 corresponding to the intrinsic operators of Python. For example,
11 \code{operator.add(x, y)} is equivalent to the expression \code{x+y}. The
12 function names are those used for special class methods; variants without
13 leading and trailing \samp{__} are also provided for convenience.
15 The \module{operator} module defines the following functions:
17 \begin{funcdesc}{add}{a, b}
18 \funcline{__add__}{a, b}
19 Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers.
20 \end{funcdesc}
22 \begin{funcdesc}{sub}{a, b}
23 \funcline{__sub__}{a, b}
24 Return \var{a} \code{-} \var{b}.
25 \end{funcdesc}
27 \begin{funcdesc}{mul}{a, b}
28 \funcline{__mul__}{a, b}
29 Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers.
30 \end{funcdesc}
32 \begin{funcdesc}{div}{a, b}
33 \funcline{__div__}{a, b}
34 Return \var{a} \code{/} \var{b}.
35 \end{funcdesc}
37 \begin{funcdesc}{mod}{a, b}
38 \funcline{__mod__}{a, b}
39 Return \var{a} \code{\%} \var{b}.
40 \end{funcdesc}
42 \begin{funcdesc}{neg}{o}
43 \funcline{__neg__}{o}
44 Return \var{o} negated.
45 \end{funcdesc}
47 \begin{funcdesc}{pos}{o}
48 \funcline{__pos__}{o}
49 Return \var{o} positive.
50 \end{funcdesc}
52 \begin{funcdesc}{abs}{o}
53 \funcline{__abs__}{o}
54 Return the absolute value of \var{o}.
55 \end{funcdesc}
57 \begin{funcdesc}{inv}{o}
58 \funcline{__inv__}{o}
59 Return the inverse of \var{o}.
60 \end{funcdesc}
62 \begin{funcdesc}{lshift}{a, b}
63 \funcline{__lshift__}{a, b}
64 Return \var{a} shifted left by \var{b}.
65 \end{funcdesc}
67 \begin{funcdesc}{rshift}{a, b}
68 \funcline{__rshift__}{a, b}
69 Return \var{a} shifted right by \var{b}.
70 \end{funcdesc}
72 \begin{funcdesc}{and_}{a, b}
73 \funcline{__and__}{a, b}
74 Return the bitwise and of \var{a} and \var{b}.
75 \end{funcdesc}
77 \begin{funcdesc}{or_}{a, b}
78 \funcline{__or__}{a, b}
79 Return the bitwise or of \var{a} and \var{b}.
80 \end{funcdesc}
82 \begin{funcdesc}{xor}{a, b}
83 \funcline{__xor__}{a, b}
84 Return the bitwise exclusive or of \var{a} and \var{b}.
85 \end{funcdesc}
87 \begin{funcdesc}{not_}{o}
88 \funcline{__not__}{o}
89 Return the outcome of \keyword{not} \var{o}. (Note that there is no
90 \method{__not__()} discipline for object instances; only the
91 interpreter core defines this operation.)
92 \end{funcdesc}
94 \begin{funcdesc}{truth}{o}
95 Return \code{1} if \var{o} is true, and 0 otherwise.
96 \end{funcdesc}
98 \begin{funcdesc}{concat}{a, b}
99 \funcline{__concat__}{a, b}
100 Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
101 \end{funcdesc}
103 \begin{funcdesc}{repeat}{a, b}
104 \funcline{__repeat__}{a, b}
105 Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
106 \var{b} is an integer.
107 \end{funcdesc}
109 \begin{funcdesc}{contains}{a, b}
110 \funcline{sequenceIncludes}{a, b}
111 Return the outcome of the test \var{b} \code{in} \var{a}.
112 Note the reversed operands.
113 \end{funcdesc}
115 \begin{funcdesc}{countOf}{a, b}
116 Return the number of occurrences of \var{b} in \var{a}.
117 \end{funcdesc}
119 \begin{funcdesc}{indexOf}{a, b}
120 Return the index of the first of occurrence of \var{b} in \var{a}.
121 \end{funcdesc}
123 \begin{funcdesc}{getitem}{a, b}
124 \funcline{__getitem__}{a, b}
125 Return the value of \var{a} at index \var{b}.
126 \end{funcdesc}
128 \begin{funcdesc}{setitem}{a, b, c}
129 \funcline{__setitem__}{a, b, c}
130 Set the value of \var{a} at index \var{b} to \var{c}.
131 \end{funcdesc}
133 \begin{funcdesc}{delitem}{a, b}
134 \funcline{__delitem__}{a, b}
135 Remove the value of \var{a} at index \var{b}.
136 \end{funcdesc}
138 \begin{funcdesc}{getslice}{a, b, c}
139 \funcline{__getslice__}{a, b, c}
140 Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
141 \end{funcdesc}
143 \begin{funcdesc}{setslice}{a, b, c, v}
144 \funcline{__setslice__}{a, b, c, v}
145 Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
146 sequence \var{v}.
147 \end{funcdesc}
149 \begin{funcdesc}{delslice}{a, b, c}
150 \funcline{__delslice__}{a, b, c}
151 Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
152 \end{funcdesc}
155 Example: Build a dictionary that maps the ordinals from \code{0} to
156 \code{256} to their character equivalents.
158 \begin{verbatim}
159 >>> import operator
160 >>> d = {}
161 >>> keys = range(256)
162 >>> vals = map(chr, keys)
163 >>> map(operator.setitem, [d]*len(keys), keys, vals)
164 \end{verbatim}