Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{operator} --- |
| 2 | Standard operators as functions.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{builtin}{operator} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 4 | \sectionauthor{Skip Montanaro}{skip@automatrix.com} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | |
| 6 | \modulesynopsis{All Python's standard operators as built-in functions.} |
| 7 | |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 8 | |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 9 | The \module{operator} module exports a set of functions implemented in C |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 10 | corresponding to the intrinsic operators of Python. For example, |
Fred Drake | f3e6df1 | 1997-12-29 17:11:55 +0000 | [diff] [blame] | 11 | \code{operator.add(x, y)} is equivalent to the expression \code{x+y}. The |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 12 | function names are those used for special class methods; variants without |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 13 | leading and trailing \samp{__} are also provided for convenience. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 14 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 15 | The functions fall into categories that perform object comparisons, |
| 16 | logical operations, mathematical operations, sequence operations, and |
| 17 | abstract type tests. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 18 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 19 | The object comparison functions are useful for all objects, and are |
| 20 | named after the rich comparison operators they support: |
| 21 | |
| 22 | \begin{funcdesc}{lt}{a, b} |
| 23 | \funcline{le}{a, b} |
| 24 | \funcline{eq}{a, b} |
| 25 | \funcline{ne}{a, b} |
| 26 | \funcline{ge}{a, b} |
| 27 | \funcline{gt}{a, b} |
| 28 | \funcline{__lt__}{a, b} |
| 29 | \funcline{__le__}{a, b} |
| 30 | \funcline{__eq__}{a, b} |
| 31 | \funcline{__ne__}{a, b} |
| 32 | \funcline{__ge__}{a, b} |
| 33 | \funcline{__gt__}{a, b} |
| 34 | Perform ``rich comparisons'' between \var{a} and \var{b}. Specifically, |
| 35 | \code{lt(\var{a}, \var{b})} is equivalent to \code{\var{a} < \var{b}}, |
| 36 | \code{le(\var{a}, \var{b})} is equivalent to \code{\var{a} <= \var{b}}, |
| 37 | \code{eq(\var{a}, \var{b})} is equivalent to \code{\var{a} == \var{b}}, |
| 38 | \code{ne(\var{a}, \var{b})} is equivalent to \code{\var{a} != \var{b}}, |
| 39 | \code{gt(\var{a}, \var{b})} is equivalent to \code{\var{a} > \var{b}} |
| 40 | and |
| 41 | \code{ge(\var{a}, \var{b})} is equivalent to \code{\var{a} >= \var{b}}. |
| 42 | Note that unlike the built-in \function{cmp()}, these functions can |
| 43 | return any value, which may or may not be interpretable as a Boolean |
| 44 | value. See the \citetitle[../ref/ref.html]{Python Reference Manual} |
| 45 | for more informations about rich comparisons. |
| 46 | \versionadded{2.2} |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 47 | \end{funcdesc} |
| 48 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 49 | |
| 50 | The logical operations are also generally applicable to all objects, |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 51 | and support truth tests, identity tests, and Boolean operations: |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 52 | |
| 53 | \begin{funcdesc}{not_}{o} |
| 54 | \funcline{__not__}{o} |
| 55 | Return the outcome of \keyword{not} \var{o}. (Note that there is no |
| 56 | \method{__not__()} method for object instances; only the interpreter |
| 57 | core defines this operation. The result is affected by the |
| 58 | \method{__nonzero__()} and \method{__len__()} methods.) |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 59 | \end{funcdesc} |
| 60 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 61 | \begin{funcdesc}{truth}{o} |
Fred Drake | 8ec17a0 | 2003-01-11 23:15:47 +0000 | [diff] [blame] | 62 | Return \constant{True} if \var{o} is true, and \constant{False} |
Neal Norwitz | 06daee9 | 2003-01-12 15:04:54 +0000 | [diff] [blame] | 63 | otherwise. This is equivalent to using the \class{bool} |
Fred Drake | 8ec17a0 | 2003-01-11 23:15:47 +0000 | [diff] [blame] | 64 | constructor. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 65 | \end{funcdesc} |
| 66 | |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 67 | \begin{funcdesc}{is_}{a, b} |
| 68 | Return \code{\var{a} is \var{b}}. Tests object identity. |
| 69 | \end{funcdesc} |
| 70 | |
| 71 | \begin{funcdesc}{is_not}{a, b} |
| 72 | Return \code{\var{a} is not \var{b}}. Tests object identity. |
| 73 | \end{funcdesc} |
| 74 | |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 75 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 76 | The mathematical and bitwise operations are the most numerous: |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 77 | |
| 78 | \begin{funcdesc}{abs}{o} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 79 | \funcline{__abs__}{o} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 80 | Return the absolute value of \var{o}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 81 | \end{funcdesc} |
| 82 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 83 | \begin{funcdesc}{add}{a, b} |
| 84 | \funcline{__add__}{a, b} |
| 85 | Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 86 | \end{funcdesc} |
| 87 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 88 | \begin{funcdesc}{and_}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 89 | \funcline{__and__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 90 | Return the bitwise and of \var{a} and \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 91 | \end{funcdesc} |
| 92 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 93 | \begin{funcdesc}{div}{a, b} |
| 94 | \funcline{__div__}{a, b} |
| 95 | Return \var{a} \code{/} \var{b} when \code{__future__.division} is not |
| 96 | in effect. This is also known as ``classic'' division. |
| 97 | \end{funcdesc} |
| 98 | |
| 99 | \begin{funcdesc}{floordiv}{a, b} |
| 100 | \funcline{__floordiv__}{a, b} |
| 101 | Return \var{a} \code{//} \var{b}. |
| 102 | \versionadded{2.2} |
| 103 | \end{funcdesc} |
| 104 | |
| 105 | \begin{funcdesc}{inv}{o} |
| 106 | \funcline{invert}{o} |
| 107 | \funcline{__inv__}{o} |
| 108 | \funcline{__invert__}{o} |
| 109 | Return the bitwise inverse of the number \var{o}. This is equivalent |
| 110 | to \code{\textasciitilde}\var{o}. The names \function{invert()} and |
| 111 | \function{__invert__()} were added in Python 2.0. |
| 112 | \end{funcdesc} |
| 113 | |
| 114 | \begin{funcdesc}{lshift}{a, b} |
| 115 | \funcline{__lshift__}{a, b} |
| 116 | Return \var{a} shifted left by \var{b}. |
| 117 | \end{funcdesc} |
| 118 | |
| 119 | \begin{funcdesc}{mod}{a, b} |
| 120 | \funcline{__mod__}{a, b} |
| 121 | Return \var{a} \code{\%} \var{b}. |
| 122 | \end{funcdesc} |
| 123 | |
| 124 | \begin{funcdesc}{mul}{a, b} |
| 125 | \funcline{__mul__}{a, b} |
| 126 | Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers. |
| 127 | \end{funcdesc} |
| 128 | |
| 129 | \begin{funcdesc}{neg}{o} |
| 130 | \funcline{__neg__}{o} |
| 131 | Return \var{o} negated. |
| 132 | \end{funcdesc} |
| 133 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 134 | \begin{funcdesc}{or_}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 135 | \funcline{__or__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 136 | Return the bitwise or of \var{a} and \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 137 | \end{funcdesc} |
| 138 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 139 | \begin{funcdesc}{pos}{o} |
| 140 | \funcline{__pos__}{o} |
| 141 | Return \var{o} positive. |
| 142 | \end{funcdesc} |
| 143 | |
Raymond Hettinger | 5959c55 | 2002-08-19 03:19:09 +0000 | [diff] [blame] | 144 | \begin{funcdesc}{pow}{a, b} |
| 145 | \funcline{__pow__}{a, b} |
| 146 | Return \var{a} \code{**} \var{b}, for \var{a} and \var{b} numbers. |
Neal Norwitz | 11b795c | 2002-08-19 22:38:01 +0000 | [diff] [blame] | 147 | \versionadded{2.3} |
Raymond Hettinger | 5959c55 | 2002-08-19 03:19:09 +0000 | [diff] [blame] | 148 | \end{funcdesc} |
| 149 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 150 | \begin{funcdesc}{rshift}{a, b} |
| 151 | \funcline{__rshift__}{a, b} |
| 152 | Return \var{a} shifted right by \var{b}. |
| 153 | \end{funcdesc} |
| 154 | |
| 155 | \begin{funcdesc}{sub}{a, b} |
| 156 | \funcline{__sub__}{a, b} |
| 157 | Return \var{a} \code{-} \var{b}. |
| 158 | \end{funcdesc} |
| 159 | |
| 160 | \begin{funcdesc}{truediv}{a, b} |
| 161 | \funcline{__truediv__}{a, b} |
| 162 | Return \var{a} \code{/} \var{b} when \code{__future__.division} is in |
| 163 | effect. This is also known as division. |
| 164 | \versionadded{2.2} |
| 165 | \end{funcdesc} |
| 166 | |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 167 | \begin{funcdesc}{xor}{a, b} |
| 168 | \funcline{__xor__}{a, b} |
| 169 | Return the bitwise exclusive or of \var{a} and \var{b}. |
| 170 | \end{funcdesc} |
| 171 | |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 172 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 173 | Operations which work with sequences include: |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 174 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 175 | \begin{funcdesc}{concat}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 176 | \funcline{__concat__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 177 | Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 178 | \end{funcdesc} |
| 179 | |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 180 | \begin{funcdesc}{contains}{a, b} |
Fred Drake | 5316ef4 | 2000-09-17 16:10:25 +0000 | [diff] [blame] | 181 | \funcline{__contains__}{a, b} |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 182 | Return the outcome of the test \var{b} \code{in} \var{a}. |
Fred Drake | 5316ef4 | 2000-09-17 16:10:25 +0000 | [diff] [blame] | 183 | Note the reversed operands. The name \function{__contains__()} was |
| 184 | added in Python 2.0. |
| 185 | \end{funcdesc} |
| 186 | |
Guido van Rossum | a58e9ed | 1998-05-22 18:48:37 +0000 | [diff] [blame] | 187 | \begin{funcdesc}{countOf}{a, b} |
| 188 | Return the number of occurrences of \var{b} in \var{a}. |
| 189 | \end{funcdesc} |
| 190 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 191 | \begin{funcdesc}{delitem}{a, b} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 192 | \funcline{__delitem__}{a, b} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 193 | Remove the value of \var{a} at index \var{b}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 194 | \end{funcdesc} |
| 195 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 196 | \begin{funcdesc}{delslice}{a, b, c} |
| 197 | \funcline{__delslice__}{a, b, c} |
| 198 | Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}. |
| 199 | \end{funcdesc} |
| 200 | |
| 201 | \begin{funcdesc}{getitem}{a, b} |
| 202 | \funcline{__getitem__}{a, b} |
| 203 | Return the value of \var{a} at index \var{b}. |
| 204 | \end{funcdesc} |
| 205 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 206 | \begin{funcdesc}{getslice}{a, b, c} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 207 | \funcline{__getslice__}{a, b, c} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 208 | Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 209 | \end{funcdesc} |
| 210 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 211 | \begin{funcdesc}{indexOf}{a, b} |
| 212 | Return the index of the first of occurrence of \var{b} in \var{a}. |
| 213 | \end{funcdesc} |
| 214 | |
| 215 | \begin{funcdesc}{repeat}{a, b} |
| 216 | \funcline{__repeat__}{a, b} |
| 217 | Return \var{a} \code{*} \var{b} where \var{a} is a sequence and |
| 218 | \var{b} is an integer. |
| 219 | \end{funcdesc} |
| 220 | |
| 221 | \begin{funcdesc}{sequenceIncludes}{\unspecified} |
| 222 | \deprecated{2.0}{Use \function{contains()} instead.} |
| 223 | Alias for \function{contains()}. |
| 224 | \end{funcdesc} |
| 225 | |
| 226 | \begin{funcdesc}{setitem}{a, b, c} |
| 227 | \funcline{__setitem__}{a, b, c} |
| 228 | Set the value of \var{a} at index \var{b} to \var{c}. |
| 229 | \end{funcdesc} |
| 230 | |
Fred Drake | 98b032a | 1997-12-04 14:20:59 +0000 | [diff] [blame] | 231 | \begin{funcdesc}{setslice}{a, b, c, v} |
Fred Drake | c07ae9f | 1998-03-08 05:56:15 +0000 | [diff] [blame] | 232 | \funcline{__setslice__}{a, b, c, v} |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 233 | Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the |
| 234 | sequence \var{v}. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 235 | \end{funcdesc} |
| 236 | |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 237 | |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 238 | The \module{operator} module also defines a few predicates to test the |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 239 | type of objects. \note{Be careful not to misinterpret the |
Fred Drake | 8d3312f | 2000-10-02 03:36:18 +0000 | [diff] [blame] | 240 | results of these functions; only \function{isCallable()} has any |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 241 | measure of reliability with instance objects. For example:} |
Fred Drake | 8d3312f | 2000-10-02 03:36:18 +0000 | [diff] [blame] | 242 | |
| 243 | \begin{verbatim} |
| 244 | >>> class C: |
| 245 | ... pass |
| 246 | ... |
| 247 | >>> import operator |
| 248 | >>> o = C() |
| 249 | >>> operator.isMappingType(o) |
| 250 | 1 |
| 251 | \end{verbatim} |
| 252 | |
| 253 | \begin{funcdesc}{isCallable}{o} |
| 254 | \deprecated{2.0}{Use the \function{callable()} built-in function instead.} |
| 255 | Returns true if the object \var{o} can be called like a function, |
| 256 | otherwise it returns false. True is returned for functions, bound and |
| 257 | unbound methods, class objects, and instance objects which support the |
| 258 | \method{__call__()} method. |
| 259 | \end{funcdesc} |
| 260 | |
| 261 | \begin{funcdesc}{isMappingType}{o} |
| 262 | Returns true if the object \var{o} supports the mapping interface. |
| 263 | This is true for dictionaries and all instance objects. |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 264 | \warning{There is no reliable way to test if an instance |
Fred Drake | 8d3312f | 2000-10-02 03:36:18 +0000 | [diff] [blame] | 265 | supports the complete mapping protocol since the interface itself is |
| 266 | ill-defined. This makes this test less useful than it otherwise might |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 267 | be.} |
Fred Drake | 8d3312f | 2000-10-02 03:36:18 +0000 | [diff] [blame] | 268 | \end{funcdesc} |
| 269 | |
| 270 | \begin{funcdesc}{isNumberType}{o} |
| 271 | Returns true if the object \var{o} represents a number. This is true |
| 272 | for all numeric types implemented in C, and for all instance objects. |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 273 | \warning{There is no reliable way to test if an instance |
Fred Drake | 8d3312f | 2000-10-02 03:36:18 +0000 | [diff] [blame] | 274 | supports the complete numeric interface since the interface itself is |
| 275 | ill-defined. This makes this test less useful than it otherwise might |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 276 | be.} |
Fred Drake | 8d3312f | 2000-10-02 03:36:18 +0000 | [diff] [blame] | 277 | \end{funcdesc} |
| 278 | |
| 279 | \begin{funcdesc}{isSequenceType}{o} |
| 280 | Returns true if the object \var{o} supports the sequence protocol. |
| 281 | This returns true for all objects which define sequence methods in C, |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 282 | and for all instance objects. \warning{There is no reliable |
Fred Drake | 8d3312f | 2000-10-02 03:36:18 +0000 | [diff] [blame] | 283 | way to test if an instance supports the complete sequence interface |
| 284 | since the interface itself is ill-defined. This makes this test less |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 285 | useful than it otherwise might be.} |
Fred Drake | 8d3312f | 2000-10-02 03:36:18 +0000 | [diff] [blame] | 286 | \end{funcdesc} |
| 287 | |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 288 | |
Fred Drake | 0514ce1 | 1997-12-16 14:29:48 +0000 | [diff] [blame] | 289 | Example: Build a dictionary that maps the ordinals from \code{0} to |
| 290 | \code{256} to their character equivalents. |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 291 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 292 | \begin{verbatim} |
Guido van Rossum | 61ed4db | 1996-12-06 21:22:41 +0000 | [diff] [blame] | 293 | >>> import operator |
| 294 | >>> d = {} |
| 295 | >>> keys = range(256) |
| 296 | >>> vals = map(chr, keys) |
| 297 | >>> map(operator.setitem, [d]*len(keys), keys, vals) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 298 | \end{verbatim} |
Fred Drake | 8c2fd49 | 2000-10-22 03:19:30 +0000 | [diff] [blame] | 299 | |
| 300 | |
| 301 | \subsection{Mapping Operators to Functions \label{operator-map}} |
| 302 | |
| 303 | This table shows how abstract operations correspond to operator |
| 304 | symbols in the Python syntax and the functions in the |
| 305 | \refmodule{operator} module. |
| 306 | |
| 307 | |
| 308 | \begin{tableiii}{l|c|l}{textrm}{Operation}{Syntax}{Function} |
| 309 | \lineiii{Addition}{\code{\var{a} + \var{b}}} |
| 310 | {\code{add(\var{a}, \var{b})}} |
| 311 | \lineiii{Concatenation}{\code{\var{seq1} + \var{seq2}}} |
| 312 | {\code{concat(\var{seq1}, \var{seq2})}} |
| 313 | \lineiii{Containment Test}{\code{\var{o} in \var{seq}}} |
| 314 | {\code{contains(\var{seq}, \var{o})}} |
| 315 | \lineiii{Division}{\code{\var{a} / \var{b}}} |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 316 | {\code{div(\var{a}, \var{b}) \#} without \code{__future__.division}} |
| 317 | \lineiii{Division}{\code{\var{a} / \var{b}}} |
| 318 | {\code{truediv(\var{a}, \var{b}) \#} with \code{__future__.division}} |
| 319 | \lineiii{Division}{\code{\var{a} // \var{b}}} |
| 320 | {\code{floordiv(\var{a}, \var{b})}} |
Fred Drake | 8c2fd49 | 2000-10-22 03:19:30 +0000 | [diff] [blame] | 321 | \lineiii{Bitwise And}{\code{\var{a} \&\ \var{b}}} |
| 322 | {\code{and_(\var{a}, \var{b})}} |
| 323 | \lineiii{Bitwise Exclusive Or}{\code{\var{a} \^\ \var{b}}} |
| 324 | {\code{xor(\var{a}, \var{b})}} |
| 325 | \lineiii{Bitwise Inversion}{\code{\~{} \var{a}}} |
| 326 | {\code{invert(\var{a})}} |
| 327 | \lineiii{Bitwise Or}{\code{\var{a} | \var{b}}} |
| 328 | {\code{or_(\var{a}, \var{b})}} |
Raymond Hettinger | 5959c55 | 2002-08-19 03:19:09 +0000 | [diff] [blame] | 329 | \lineiii{Exponentiation}{\code{\var{a} ** \var{b}}} |
| 330 | {\code{pow(\var{a}, \var{b})}} |
Fred Drake | 8c2fd49 | 2000-10-22 03:19:30 +0000 | [diff] [blame] | 331 | \lineiii{Indexed Assignment}{\code{\var{o}[\var{k}] = \var{v}}} |
| 332 | {\code{setitem(\var{o}, \var{k}, \var{v})}} |
| 333 | \lineiii{Indexed Deletion}{\code{del \var{o}[\var{k}]}} |
| 334 | {\code{delitem(\var{o}, \var{k})}} |
| 335 | \lineiii{Indexing}{\code{\var{o}[\var{k}]}} |
| 336 | {\code{getitem(\var{o}, \var{k})}} |
| 337 | \lineiii{Left Shift}{\code{\var{a} <\code{<} \var{b}}} |
| 338 | {\code{lshift(\var{a}, \var{b})}} |
| 339 | \lineiii{Modulo}{\code{\var{a} \%\ \var{b}}} |
| 340 | {\code{mod(\var{a}, \var{b})}} |
| 341 | \lineiii{Multiplication}{\code{\var{a} * \var{b}}} |
| 342 | {\code{mul(\var{a}, \var{b})}} |
| 343 | \lineiii{Negation (Arithmetic)}{\code{- \var{a}}} |
| 344 | {\code{neg(\var{a})}} |
| 345 | \lineiii{Negation (Logical)}{\code{not \var{a}}} |
| 346 | {\code{not_(\var{a})}} |
| 347 | \lineiii{Right Shift}{\code{\var{a} >\code{>} \var{b}}} |
| 348 | {\code{rshift(\var{a}, \var{b})}} |
| 349 | \lineiii{Sequence Repitition}{\code{\var{seq} * \var{i}}} |
| 350 | {\code{repeat(\var{seq}, \var{i})}} |
| 351 | \lineiii{Slice Assignment}{\code{\var{seq}[\var{i}:\var{j}]} = \var{values}} |
| 352 | {\code{setslice(\var{seq}, \var{i}, \var{j}, \var{values})}} |
| 353 | \lineiii{Slice Deletion}{\code{del \var{seq}[\var{i}:\var{j}]}} |
| 354 | {\code{delslice(\var{seq}, \var{i}, \var{j})}} |
| 355 | \lineiii{Slicing}{\code{\var{seq}[\var{i}:\var{j}]}} |
| 356 | {\code{getslice(\var{seq}, \var{i}, \var{j})}} |
| 357 | \lineiii{String Formatting}{\code{\var{s} \%\ \var{o}}} |
| 358 | {\code{mod(\var{s}, \var{o})}} |
| 359 | \lineiii{Subtraction}{\code{\var{a} - \var{b}}} |
| 360 | {\code{sub(\var{a}, \var{b})}} |
| 361 | \lineiii{Truth Test}{\code{\var{o}}} |
| 362 | {\code{truth(\var{o})}} |
Fred Drake | e89659c | 2001-08-10 15:55:09 +0000 | [diff] [blame] | 363 | \lineiii{Ordering}{\code{\var{a} < \var{b}}} |
| 364 | {\code{lt(\var{a}, \var{b})}} |
| 365 | \lineiii{Ordering}{\code{\var{a} <= \var{b}}} |
| 366 | {\code{le(\var{a}, \var{b})}} |
| 367 | \lineiii{Equality}{\code{\var{a} == \var{b}}} |
| 368 | {\code{eq(\var{a}, \var{b})}} |
| 369 | \lineiii{Difference}{\code{\var{a} != \var{b}}} |
| 370 | {\code{ne(\var{a}, \var{b})}} |
| 371 | \lineiii{Ordering}{\code{\var{a} >= \var{b}}} |
| 372 | {\code{ge(\var{a}, \var{b})}} |
| 373 | \lineiii{Ordering}{\code{\var{a} > \var{b}}} |
| 374 | {\code{gt(\var{a}, \var{b})}} |
Fred Drake | 8c2fd49 | 2000-10-22 03:19:30 +0000 | [diff] [blame] | 375 | \end{tableiii} |