|
| 1 | +% Contributed by Skip Montanaro, from the module's doc strings. |
| 2 | + |
| 3 | +\section{Built-in Module \sectcode{operator}} % If implemented in C |
| 4 | +\bimodindex{spam} |
| 5 | + |
| 6 | +The \code{operator} module exports a set of functions implemented in C |
| 7 | +corresponding to the intrinsic operators of Python. For example, |
| 8 | +{}\code{operator.add(x, y)} is equivalent to the expression x+y. The |
| 9 | +function names are those used for special class methods; variants without |
| 10 | +leading and trailing '\_\_' are also provided for convenience. |
| 11 | + |
| 12 | +The \code{operator} module defines the following functions: |
| 13 | + |
| 14 | +\renewcommand{\indexsubitem}{(in module operator)} |
| 15 | + |
| 16 | +\begin{funcdesc}{add}{a\, b} |
| 17 | +Return a + b, for a and b numbers. |
| 18 | +\end{funcdesc} |
| 19 | + |
| 20 | +\begin{funcdesc}{\_\_add\_\_}{a\, b} |
| 21 | +Return a + b, for a and b numbers. |
| 22 | +\end{funcdesc} |
| 23 | + |
| 24 | +\begin{funcdesc}{sub}{a\, b} |
| 25 | +Return a - b. |
| 26 | +\end{funcdesc} |
| 27 | + |
| 28 | +\begin{funcdesc}{\_\_sub\_\_}{a\, b} |
| 29 | +Return a - b. |
| 30 | +\end{funcdesc} |
| 31 | + |
| 32 | +\begin{funcdesc}{mul}{a\, b} |
| 33 | +Return a * b, for a and b numbers. |
| 34 | +\end{funcdesc} |
| 35 | + |
| 36 | +\begin{funcdesc}{\_\_mul\_\_}{a\, b} |
| 37 | +Return a * b, for a and b numbers. |
| 38 | +\end{funcdesc} |
| 39 | + |
| 40 | +\begin{funcdesc}{div}{a\, b} |
| 41 | +Return a / b. |
| 42 | +\end{funcdesc} |
| 43 | + |
| 44 | +\begin{funcdesc}{\_\_div\_\_}{a\, b} |
| 45 | +Return a / b. |
| 46 | +\end{funcdesc} |
| 47 | + |
| 48 | +\begin{funcdesc}{mod}{a\, b} |
| 49 | +Return a \% b. |
| 50 | +\end{funcdesc} |
| 51 | + |
| 52 | +\begin{funcdesc}{\_\_mod\_\_}{a\, b} |
| 53 | +Return a \% b. |
| 54 | +\end{funcdesc} |
| 55 | + |
| 56 | +\begin{funcdesc}{neg}{o} |
| 57 | +Return o negated. |
| 58 | +\end{funcdesc} |
| 59 | + |
| 60 | +\begin{funcdesc}{\_\_neg\_\_}{o} |
| 61 | +Return o negated. |
| 62 | +\end{funcdesc} |
| 63 | + |
| 64 | +\begin{funcdesc}{pos}{o} |
| 65 | +Return o positive. |
| 66 | +\end{funcdesc} |
| 67 | + |
| 68 | +\begin{funcdesc}{\_\_pos\_\_}{o} |
| 69 | +Return o positive. |
| 70 | +\end{funcdesc} |
| 71 | + |
| 72 | +\begin{funcdesc}{abs}{o} |
| 73 | +Return the absolute value of o. |
| 74 | +\end{funcdesc} |
| 75 | + |
| 76 | +\begin{funcdesc}{\_\_abs\_\_}{o} |
| 77 | +Return the absolute value of o. |
| 78 | +\end{funcdesc} |
| 79 | + |
| 80 | +\begin{funcdesc}{inv}{o} |
| 81 | +Return the inverse of o. |
| 82 | +\end{funcdesc} |
| 83 | + |
| 84 | +\begin{funcdesc}{\_\_inv\_\_}{o} |
| 85 | +Return the inverse of o. |
| 86 | +\end{funcdesc} |
| 87 | + |
| 88 | +\begin{funcdesc}{lshift}{a\, b} |
| 89 | +Return a shifted left by b. |
| 90 | +\end{funcdesc} |
| 91 | + |
| 92 | +\begin{funcdesc}{\_\_lshift\_\_}{a\, b} |
| 93 | +Return a shifted left by b. |
| 94 | +\end{funcdesc} |
| 95 | + |
| 96 | +\begin{funcdesc}{rshift}{a\, b} |
| 97 | +Return a shifted right by b. |
| 98 | +\end{funcdesc} |
| 99 | + |
| 100 | +\begin{funcdesc}{\_\_rshift\_\_}{a\, b} |
| 101 | +Return a shifted right by b. |
| 102 | +\end{funcdesc} |
| 103 | + |
| 104 | +\begin{funcdesc}{and\_}{a\, b} |
| 105 | +Return the bitwise and of a and b. |
| 106 | +\end{funcdesc} |
| 107 | + |
| 108 | +\begin{funcdesc}{\_\_and\_\_}{a\, b} |
| 109 | +Return the bitwise and of a and b. |
| 110 | +\end{funcdesc} |
| 111 | + |
| 112 | +\begin{funcdesc}{or\_}{a\, b} |
| 113 | +Return the bitwise or of a and b. |
| 114 | +\end{funcdesc} |
| 115 | + |
| 116 | +\begin{funcdesc}{\_\_or\_\_}{a\, b} |
| 117 | +Return the bitwise or of a and b. |
| 118 | +\end{funcdesc} |
| 119 | + |
| 120 | +\begin{funcdesc}{concat}{a\, b} |
| 121 | +Return a + b for a and b sequences. |
| 122 | +\end{funcdesc} |
| 123 | + |
| 124 | +\begin{funcdesc}{\_\_concat\_\_}{a\, b} |
| 125 | +Return a + b for a and b sequences. |
| 126 | +\end{funcdesc} |
| 127 | + |
| 128 | +\begin{funcdesc}{repeat}{a\, b} |
| 129 | +Return a * b where a is a sequence and b is an integer. |
| 130 | +\end{funcdesc} |
| 131 | + |
| 132 | +\begin{funcdesc}{\_\_repeat\_\_}{a\, b} |
| 133 | +Return a * b where a is a sequence and b is an integer. |
| 134 | +\end{funcdesc} |
| 135 | + |
| 136 | +\begin{funcdesc}{getitem}{a\, b} |
| 137 | +Return the value of a at index b. |
| 138 | +\end{funcdesc} |
| 139 | + |
| 140 | +\begin{funcdesc}{\_\_getitem\_\_}{a\, b} |
| 141 | +Return the value of a at index b. |
| 142 | +\end{funcdesc} |
| 143 | + |
| 144 | +\begin{funcdesc}{setitem}{a\, b\, c} |
| 145 | +Set the value of a at index b to c. |
| 146 | +\end{funcdesc} |
| 147 | + |
| 148 | +\begin{funcdesc}{\_\_setitem\_\_}{a\, b\, c} |
| 149 | +Set the value of a at index b to c. |
| 150 | +\end{funcdesc} |
| 151 | + |
| 152 | +\begin{funcdesc}{delitem}{a\, b} |
| 153 | +Set the value of a at index b. |
| 154 | +\end{funcdesc} |
| 155 | + |
| 156 | +\begin{funcdesc}{\_\_delitem\_\_}{a\, b} |
| 157 | +Set the value of a at index b. |
| 158 | +\end{funcdesc} |
| 159 | + |
| 160 | +\begin{funcdesc}{getslice}{a\, b\, c} |
| 161 | +Return the slice of a from index b to index c-1. |
| 162 | +\end{funcdesc} |
| 163 | + |
| 164 | +\begin{funcdesc}{\_\_getslice\_\_}{a\, b\, c} |
| 165 | +Return the slice of a from index b to index c-1. |
| 166 | +\end{funcdesc} |
| 167 | + |
| 168 | +\begin{funcdesc}{setslice}{a\, b\, c\, v} |
| 169 | +Set the slice of a from index b to index c-1 to the sequence v. |
| 170 | +\end{funcdesc} |
| 171 | + |
| 172 | +\begin{funcdesc}{\_\_setslice\_\_}{a\, b\, c\, v} |
| 173 | +Set the slice of a from index b to index c-1 to the sequence v. |
| 174 | +\end{funcdesc} |
| 175 | + |
| 176 | +\begin{funcdesc}{delslice}{a\, b\, c} |
| 177 | +Delete the slice of a from index b to index c-1. |
| 178 | +\end{funcdesc} |
| 179 | + |
| 180 | +\begin{funcdesc}{\_\_delslice\_\_}{a\, b\, c} |
| 181 | +Delete the slice of a from index b to index c-1. |
| 182 | +\end{funcdesc} |
| 183 | + |
| 184 | +Example: Build a dictionary that maps the ordinals from 0 to 256 to their |
| 185 | +character equivalents. |
| 186 | + |
| 187 | +\begin{verbatim} |
| 188 | +>>> import operator |
| 189 | +>>> d = {} |
| 190 | +>>> keys = range(256) |
| 191 | +>>> vals = map(chr, keys) |
| 192 | +>>> map(operator.setitem, [d]*len(keys), keys, vals) |
| 193 | +\end{verbatim} |
0 commit comments