Clarify portability and main program.
[python/dscho.git] / Doc / lib / liboperator.tex
blobc32900915ab8e00934698afaf52357bcfeb99124
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}.
90 \end{funcdesc}
92 \begin{funcdesc}{truth}{o}
93 Return \code{1} if \var{o} is true, and 0 otherwise.
94 \end{funcdesc}
96 \begin{funcdesc}{concat}{a, b}
97 \funcline{__concat__}{a, b}
98 Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
99 \end{funcdesc}
101 \begin{funcdesc}{repeat}{a, b}
102 \funcline{__repeat__}{a, b}
103 Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
104 \var{b} is an integer.
105 \end{funcdesc}
107 \begin{funcdesc}{contains}{a, b}
108 \funcline{sequenceIncludes}{a, b}
109 Return the outcome of the test \var{b} \code{in} \var{a}.
110 Note the reversed operands.
111 \end{funcdesc}
113 \begin{funcdesc}{countOf}{a, b}
114 Return the number of occurrences of \var{b} in \var{a}.
115 \end{funcdesc}
117 \begin{funcdesc}{indexOf}{a, b}
118 Return the index of the first of occurrence of \var{b} in \var{a}.
119 \end{funcdesc}
121 \begin{funcdesc}{getitem}{a, b}
122 \funcline{__getitem__}{a, b}
123 Return the value of \var{a} at index \var{b}.
124 \end{funcdesc}
126 \begin{funcdesc}{setitem}{a, b, c}
127 \funcline{__setitem__}{a, b, c}
128 Set the value of \var{a} at index \var{b} to \var{c}.
129 \end{funcdesc}
131 \begin{funcdesc}{delitem}{a, b}
132 \funcline{__delitem__}{a, b}
133 Remove the value of \var{a} at index \var{b}.
134 \end{funcdesc}
136 \begin{funcdesc}{getslice}{a, b, c}
137 \funcline{__getslice__}{a, b, c}
138 Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
139 \end{funcdesc}
141 \begin{funcdesc}{setslice}{a, b, c, v}
142 \funcline{__setslice__}{a, b, c, v}
143 Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
144 sequence \var{v}.
145 \end{funcdesc}
147 \begin{funcdesc}{delslice}{a, b, c}
148 \funcline{__delslice__}{a, b, c}
149 Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
150 \end{funcdesc}
153 Example: Build a dictionary that maps the ordinals from \code{0} to
154 \code{256} to their character equivalents.
156 \begin{verbatim}
157 >>> import operator
158 >>> d = {}
159 >>> keys = range(256)
160 >>> vals = map(chr, keys)
161 >>> map(operator.setitem, [d]*len(keys), keys, vals)
162 \end{verbatim}