| 1 | \section{\module{operator} ---
|
|---|
| 2 | Standard operators as functions.}
|
|---|
| 3 | \declaremodule{builtin}{operator}
|
|---|
| 4 | \sectionauthor{Skip Montanaro}{[email protected]}
|
|---|
| 5 |
|
|---|
| 6 | \modulesynopsis{All Python's standard operators as built-in functions.}
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 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.
|
|---|
| 14 |
|
|---|
| 15 | The functions fall into categories that perform object comparisons,
|
|---|
| 16 | logical operations, mathematical operations, sequence operations, and
|
|---|
| 17 | abstract type tests.
|
|---|
| 18 |
|
|---|
| 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 information about rich comparisons.
|
|---|
| 46 | \versionadded{2.2}
|
|---|
| 47 | \end{funcdesc}
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | The logical operations are also generally applicable to all objects,
|
|---|
| 51 | and support truth tests, identity tests, and boolean operations:
|
|---|
| 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.)
|
|---|
| 59 | \end{funcdesc}
|
|---|
| 60 |
|
|---|
| 61 | \begin{funcdesc}{truth}{o}
|
|---|
| 62 | Return \constant{True} if \var{o} is true, and \constant{False}
|
|---|
| 63 | otherwise. This is equivalent to using the \class{bool}
|
|---|
| 64 | constructor.
|
|---|
| 65 | \end{funcdesc}
|
|---|
| 66 |
|
|---|
| 67 | \begin{funcdesc}{is_}{a, b}
|
|---|
| 68 | Return \code{\var{a} is \var{b}}. Tests object identity.
|
|---|
| 69 | \versionadded{2.3}
|
|---|
| 70 | \end{funcdesc}
|
|---|
| 71 |
|
|---|
| 72 | \begin{funcdesc}{is_not}{a, b}
|
|---|
| 73 | Return \code{\var{a} is not \var{b}}. Tests object identity.
|
|---|
| 74 | \versionadded{2.3}
|
|---|
| 75 | \end{funcdesc}
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | The mathematical and bitwise operations are the most numerous:
|
|---|
| 79 |
|
|---|
| 80 | \begin{funcdesc}{abs}{o}
|
|---|
| 81 | \funcline{__abs__}{o}
|
|---|
| 82 | Return the absolute value of \var{o}.
|
|---|
| 83 | \end{funcdesc}
|
|---|
| 84 |
|
|---|
| 85 | \begin{funcdesc}{add}{a, b}
|
|---|
| 86 | \funcline{__add__}{a, b}
|
|---|
| 87 | Return \var{a} \code{+} \var{b}, for \var{a} and \var{b} numbers.
|
|---|
| 88 | \end{funcdesc}
|
|---|
| 89 |
|
|---|
| 90 | \begin{funcdesc}{and_}{a, b}
|
|---|
| 91 | \funcline{__and__}{a, b}
|
|---|
| 92 | Return the bitwise and of \var{a} and \var{b}.
|
|---|
| 93 | \end{funcdesc}
|
|---|
| 94 |
|
|---|
| 95 | \begin{funcdesc}{div}{a, b}
|
|---|
| 96 | \funcline{__div__}{a, b}
|
|---|
| 97 | Return \var{a} \code{/} \var{b} when \code{__future__.division} is not
|
|---|
| 98 | in effect. This is also known as ``classic'' division.
|
|---|
| 99 | \end{funcdesc}
|
|---|
| 100 |
|
|---|
| 101 | \begin{funcdesc}{floordiv}{a, b}
|
|---|
| 102 | \funcline{__floordiv__}{a, b}
|
|---|
| 103 | Return \var{a} \code{//} \var{b}.
|
|---|
| 104 | \versionadded{2.2}
|
|---|
| 105 | \end{funcdesc}
|
|---|
| 106 |
|
|---|
| 107 | \begin{funcdesc}{inv}{o}
|
|---|
| 108 | \funcline{invert}{o}
|
|---|
| 109 | \funcline{__inv__}{o}
|
|---|
| 110 | \funcline{__invert__}{o}
|
|---|
| 111 | Return the bitwise inverse of the number \var{o}. This is equivalent
|
|---|
| 112 | to \code{\textasciitilde}\var{o}. The names \function{invert()} and
|
|---|
| 113 | \function{__invert__()} were added in Python 2.0.
|
|---|
| 114 | \end{funcdesc}
|
|---|
| 115 |
|
|---|
| 116 | \begin{funcdesc}{lshift}{a, b}
|
|---|
| 117 | \funcline{__lshift__}{a, b}
|
|---|
| 118 | Return \var{a} shifted left by \var{b}.
|
|---|
| 119 | \end{funcdesc}
|
|---|
| 120 |
|
|---|
| 121 | \begin{funcdesc}{mod}{a, b}
|
|---|
| 122 | \funcline{__mod__}{a, b}
|
|---|
| 123 | Return \var{a} \code{\%} \var{b}.
|
|---|
| 124 | \end{funcdesc}
|
|---|
| 125 |
|
|---|
| 126 | \begin{funcdesc}{mul}{a, b}
|
|---|
| 127 | \funcline{__mul__}{a, b}
|
|---|
| 128 | Return \var{a} \code{*} \var{b}, for \var{a} and \var{b} numbers.
|
|---|
| 129 | \end{funcdesc}
|
|---|
| 130 |
|
|---|
| 131 | \begin{funcdesc}{neg}{o}
|
|---|
| 132 | \funcline{__neg__}{o}
|
|---|
| 133 | Return \var{o} negated.
|
|---|
| 134 | \end{funcdesc}
|
|---|
| 135 |
|
|---|
| 136 | \begin{funcdesc}{or_}{a, b}
|
|---|
| 137 | \funcline{__or__}{a, b}
|
|---|
| 138 | Return the bitwise or of \var{a} and \var{b}.
|
|---|
| 139 | \end{funcdesc}
|
|---|
| 140 |
|
|---|
| 141 | \begin{funcdesc}{pos}{o}
|
|---|
| 142 | \funcline{__pos__}{o}
|
|---|
| 143 | Return \var{o} positive.
|
|---|
| 144 | \end{funcdesc}
|
|---|
| 145 |
|
|---|
| 146 | \begin{funcdesc}{pow}{a, b}
|
|---|
| 147 | \funcline{__pow__}{a, b}
|
|---|
| 148 | Return \var{a} \code{**} \var{b}, for \var{a} and \var{b} numbers.
|
|---|
| 149 | \versionadded{2.3}
|
|---|
| 150 | \end{funcdesc}
|
|---|
| 151 |
|
|---|
| 152 | \begin{funcdesc}{rshift}{a, b}
|
|---|
| 153 | \funcline{__rshift__}{a, b}
|
|---|
| 154 | Return \var{a} shifted right by \var{b}.
|
|---|
| 155 | \end{funcdesc}
|
|---|
| 156 |
|
|---|
| 157 | \begin{funcdesc}{sub}{a, b}
|
|---|
| 158 | \funcline{__sub__}{a, b}
|
|---|
| 159 | Return \var{a} \code{-} \var{b}.
|
|---|
| 160 | \end{funcdesc}
|
|---|
| 161 |
|
|---|
| 162 | \begin{funcdesc}{truediv}{a, b}
|
|---|
| 163 | \funcline{__truediv__}{a, b}
|
|---|
| 164 | Return \var{a} \code{/} \var{b} when \code{__future__.division} is in
|
|---|
| 165 | effect. This is also known as ``true'' division.
|
|---|
| 166 | \versionadded{2.2}
|
|---|
| 167 | \end{funcdesc}
|
|---|
| 168 |
|
|---|
| 169 | \begin{funcdesc}{xor}{a, b}
|
|---|
| 170 | \funcline{__xor__}{a, b}
|
|---|
| 171 | Return the bitwise exclusive or of \var{a} and \var{b}.
|
|---|
| 172 | \end{funcdesc}
|
|---|
| 173 |
|
|---|
| 174 | \begin{funcdesc}{index}{a}
|
|---|
| 175 | \funcline{__index__}{a}
|
|---|
| 176 | Return \var{a} converted to an integer. Equivalent to \var{a}\code{.__index__()}.
|
|---|
| 177 | \versionadded{2.5}
|
|---|
| 178 | \end{funcdesc}
|
|---|
| 179 |
|
|---|
| 180 | Operations which work with sequences include:
|
|---|
| 181 |
|
|---|
| 182 | \begin{funcdesc}{concat}{a, b}
|
|---|
| 183 | \funcline{__concat__}{a, b}
|
|---|
| 184 | Return \var{a} \code{+} \var{b} for \var{a} and \var{b} sequences.
|
|---|
| 185 | \end{funcdesc}
|
|---|
| 186 |
|
|---|
| 187 | \begin{funcdesc}{contains}{a, b}
|
|---|
| 188 | \funcline{__contains__}{a, b}
|
|---|
| 189 | Return the outcome of the test \var{b} \code{in} \var{a}.
|
|---|
| 190 | Note the reversed operands. The name \function{__contains__()} was
|
|---|
| 191 | added in Python 2.0.
|
|---|
| 192 | \end{funcdesc}
|
|---|
| 193 |
|
|---|
| 194 | \begin{funcdesc}{countOf}{a, b}
|
|---|
| 195 | Return the number of occurrences of \var{b} in \var{a}.
|
|---|
| 196 | \end{funcdesc}
|
|---|
| 197 |
|
|---|
| 198 | \begin{funcdesc}{delitem}{a, b}
|
|---|
| 199 | \funcline{__delitem__}{a, b}
|
|---|
| 200 | Remove the value of \var{a} at index \var{b}.
|
|---|
| 201 | \end{funcdesc}
|
|---|
| 202 |
|
|---|
| 203 | \begin{funcdesc}{delslice}{a, b, c}
|
|---|
| 204 | \funcline{__delslice__}{a, b, c}
|
|---|
| 205 | Delete the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
|
|---|
| 206 | \end{funcdesc}
|
|---|
| 207 |
|
|---|
| 208 | \begin{funcdesc}{getitem}{a, b}
|
|---|
| 209 | \funcline{__getitem__}{a, b}
|
|---|
| 210 | Return the value of \var{a} at index \var{b}.
|
|---|
| 211 | \end{funcdesc}
|
|---|
| 212 |
|
|---|
| 213 | \begin{funcdesc}{getslice}{a, b, c}
|
|---|
| 214 | \funcline{__getslice__}{a, b, c}
|
|---|
| 215 | Return the slice of \var{a} from index \var{b} to index \var{c}\code{-1}.
|
|---|
| 216 | \end{funcdesc}
|
|---|
| 217 |
|
|---|
| 218 | \begin{funcdesc}{indexOf}{a, b}
|
|---|
| 219 | Return the index of the first of occurrence of \var{b} in \var{a}.
|
|---|
| 220 | \end{funcdesc}
|
|---|
| 221 |
|
|---|
| 222 | \begin{funcdesc}{repeat}{a, b}
|
|---|
| 223 | \funcline{__repeat__}{a, b}
|
|---|
| 224 | Return \var{a} \code{*} \var{b} where \var{a} is a sequence and
|
|---|
| 225 | \var{b} is an integer.
|
|---|
| 226 | \end{funcdesc}
|
|---|
| 227 |
|
|---|
| 228 | \begin{funcdesc}{sequenceIncludes}{\unspecified}
|
|---|
| 229 | \deprecated{2.0}{Use \function{contains()} instead.}
|
|---|
| 230 | Alias for \function{contains()}.
|
|---|
| 231 | \end{funcdesc}
|
|---|
| 232 |
|
|---|
| 233 | \begin{funcdesc}{setitem}{a, b, c}
|
|---|
| 234 | \funcline{__setitem__}{a, b, c}
|
|---|
| 235 | Set the value of \var{a} at index \var{b} to \var{c}.
|
|---|
| 236 | \end{funcdesc}
|
|---|
| 237 |
|
|---|
| 238 | \begin{funcdesc}{setslice}{a, b, c, v}
|
|---|
| 239 | \funcline{__setslice__}{a, b, c, v}
|
|---|
| 240 | Set the slice of \var{a} from index \var{b} to index \var{c}\code{-1} to the
|
|---|
| 241 | sequence \var{v}.
|
|---|
| 242 | \end{funcdesc}
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 | Many operations have an ``in-place'' version. The following functions
|
|---|
| 246 | provide a more primitive access to in-place operators than the usual
|
|---|
| 247 | syntax does; for example, the statement \code{x += y} is equivalent to
|
|---|
| 248 | \code{x = operator.iadd(x, y)}. Another way to put it is to say that
|
|---|
| 249 | \code{z = operator.iadd(x, y)} is equivalent to the compound statement
|
|---|
| 250 | \code{z = x; z += y}.
|
|---|
| 251 |
|
|---|
| 252 | \begin{funcdesc}{iadd}{a, b}
|
|---|
| 253 | \funcline{__iadd__}{a, b}
|
|---|
| 254 | \code{a = iadd(a, b)} is equivalent to \code{a += b}.
|
|---|
| 255 | \versionadded{2.5}
|
|---|
| 256 | \end{funcdesc}
|
|---|
| 257 |
|
|---|
| 258 | \begin{funcdesc}{iand}{a, b}
|
|---|
| 259 | \funcline{__iand__}{a, b}
|
|---|
| 260 | \code{a = iand(a, b)} is equivalent to \code{a \&= b}.
|
|---|
| 261 | \versionadded{2.5}
|
|---|
| 262 | \end{funcdesc}
|
|---|
| 263 |
|
|---|
| 264 | \begin{funcdesc}{iconcat}{a, b}
|
|---|
| 265 | \funcline{__iconcat__}{a, b}
|
|---|
| 266 | \code{a = iconcat(a, b)} is equivalent to \code{a += b} for \var{a}
|
|---|
| 267 | and \var{b} sequences.
|
|---|
| 268 | \versionadded{2.5}
|
|---|
| 269 | \end{funcdesc}
|
|---|
| 270 |
|
|---|
| 271 | \begin{funcdesc}{idiv}{a, b}
|
|---|
| 272 | \funcline{__idiv__}{a, b}
|
|---|
| 273 | \code{a = idiv(a, b)} is equivalent to \code{a /= b} when
|
|---|
| 274 | \code{__future__.division} is not in effect.
|
|---|
| 275 | \versionadded{2.5}
|
|---|
| 276 | \end{funcdesc}
|
|---|
| 277 |
|
|---|
| 278 | \begin{funcdesc}{ifloordiv}{a, b}
|
|---|
| 279 | \funcline{__ifloordiv__}{a, b}
|
|---|
| 280 | \code{a = ifloordiv(a, b)} is equivalent to \code{a //= b}.
|
|---|
| 281 | \versionadded{2.5}
|
|---|
| 282 | \end{funcdesc}
|
|---|
| 283 |
|
|---|
| 284 | \begin{funcdesc}{ilshift}{a, b}
|
|---|
| 285 | \funcline{__ilshift__}{a, b}
|
|---|
| 286 | \code{a = ilshift(a, b)} is equivalent to \code{a <}\code{<= b}.
|
|---|
| 287 | \versionadded{2.5}
|
|---|
| 288 | \end{funcdesc}
|
|---|
| 289 |
|
|---|
| 290 | \begin{funcdesc}{imod}{a, b}
|
|---|
| 291 | \funcline{__imod__}{a, b}
|
|---|
| 292 | \code{a = imod(a, b)} is equivalent to \code{a \%= b}.
|
|---|
| 293 | \versionadded{2.5}
|
|---|
| 294 | \end{funcdesc}
|
|---|
| 295 |
|
|---|
| 296 | \begin{funcdesc}{imul}{a, b}
|
|---|
| 297 | \funcline{__imul__}{a, b}
|
|---|
| 298 | \code{a = imul(a, b)} is equivalent to \code{a *= b}.
|
|---|
| 299 | \versionadded{2.5}
|
|---|
| 300 | \end{funcdesc}
|
|---|
| 301 |
|
|---|
| 302 | \begin{funcdesc}{ior}{a, b}
|
|---|
| 303 | \funcline{__ior__}{a, b}
|
|---|
| 304 | \code{a = ior(a, b)} is equivalent to \code{a |= b}.
|
|---|
| 305 | \versionadded{2.5}
|
|---|
| 306 | \end{funcdesc}
|
|---|
| 307 |
|
|---|
| 308 | \begin{funcdesc}{ipow}{a, b}
|
|---|
| 309 | \funcline{__ipow__}{a, b}
|
|---|
| 310 | \code{a = ipow(a, b)} is equivalent to \code{a **= b}.
|
|---|
| 311 | \versionadded{2.5}
|
|---|
| 312 | \end{funcdesc}
|
|---|
| 313 |
|
|---|
| 314 | \begin{funcdesc}{irepeat}{a, b}
|
|---|
| 315 | \funcline{__irepeat__}{a, b}
|
|---|
| 316 | \code{a = irepeat(a, b)} is equivalent to \code{a *= b} where
|
|---|
| 317 | \var{a} is a sequence and \var{b} is an integer.
|
|---|
| 318 | \versionadded{2.5}
|
|---|
| 319 | \end{funcdesc}
|
|---|
| 320 |
|
|---|
| 321 | \begin{funcdesc}{irshift}{a, b}
|
|---|
| 322 | \funcline{__irshift__}{a, b}
|
|---|
| 323 | \code{a = irshift(a, b)} is equivalent to \code{a >>= b}.
|
|---|
| 324 | \versionadded{2.5}
|
|---|
| 325 | \end{funcdesc}
|
|---|
| 326 |
|
|---|
| 327 | \begin{funcdesc}{isub}{a, b}
|
|---|
| 328 | \funcline{__isub__}{a, b}
|
|---|
| 329 | \code{a = isub(a, b)} is equivalent to \code{a -= b}.
|
|---|
| 330 | \versionadded{2.5}
|
|---|
| 331 | \end{funcdesc}
|
|---|
| 332 |
|
|---|
| 333 | \begin{funcdesc}{itruediv}{a, b}
|
|---|
| 334 | \funcline{__itruediv__}{a, b}
|
|---|
| 335 | \code{a = itruediv(a, b)} is equivalent to \code{a /= b} when
|
|---|
| 336 | \code{__future__.division} is in effect.
|
|---|
| 337 | \versionadded{2.5}
|
|---|
| 338 | \end{funcdesc}
|
|---|
| 339 |
|
|---|
| 340 | \begin{funcdesc}{ixor}{a, b}
|
|---|
| 341 | \funcline{__ixor__}{a, b}
|
|---|
| 342 | \code{a = ixor(a, b)} is equivalent to \code{a \textasciicircum= b}.
|
|---|
| 343 | \versionadded{2.5}
|
|---|
| 344 | \end{funcdesc}
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 | The \module{operator} module also defines a few predicates to test the
|
|---|
| 348 | type of objects. \note{Be careful not to misinterpret the
|
|---|
| 349 | results of these functions; only \function{isCallable()} has any
|
|---|
| 350 | measure of reliability with instance objects. For example:}
|
|---|
| 351 |
|
|---|
| 352 | \begin{verbatim}
|
|---|
| 353 | >>> class C:
|
|---|
| 354 | ... pass
|
|---|
| 355 | ...
|
|---|
| 356 | >>> import operator
|
|---|
| 357 | >>> o = C()
|
|---|
| 358 | >>> operator.isMappingType(o)
|
|---|
| 359 | True
|
|---|
| 360 | \end{verbatim}
|
|---|
| 361 |
|
|---|
| 362 | \begin{funcdesc}{isCallable}{o}
|
|---|
| 363 | \deprecated{2.0}{Use the \function{callable()} built-in function instead.}
|
|---|
| 364 | Returns true if the object \var{o} can be called like a function,
|
|---|
| 365 | otherwise it returns false. True is returned for functions, bound and
|
|---|
| 366 | unbound methods, class objects, and instance objects which support the
|
|---|
| 367 | \method{__call__()} method.
|
|---|
| 368 | \end{funcdesc}
|
|---|
| 369 |
|
|---|
| 370 | \begin{funcdesc}{isMappingType}{o}
|
|---|
| 371 | Returns true if the object \var{o} supports the mapping interface.
|
|---|
| 372 | This is true for dictionaries and all instance objects defining
|
|---|
| 373 | \method{__getitem__}.
|
|---|
| 374 | \warning{There is no reliable way to test if an instance
|
|---|
| 375 | supports the complete mapping protocol since the interface itself is
|
|---|
| 376 | ill-defined. This makes this test less useful than it otherwise might
|
|---|
| 377 | be.}
|
|---|
| 378 | \end{funcdesc}
|
|---|
| 379 |
|
|---|
| 380 | \begin{funcdesc}{isNumberType}{o}
|
|---|
| 381 | Returns true if the object \var{o} represents a number. This is true
|
|---|
| 382 | for all numeric types implemented in C.
|
|---|
| 383 | \warning{There is no reliable way to test if an instance
|
|---|
| 384 | supports the complete numeric interface since the interface itself is
|
|---|
| 385 | ill-defined. This makes this test less useful than it otherwise might
|
|---|
| 386 | be.}
|
|---|
| 387 | \end{funcdesc}
|
|---|
| 388 |
|
|---|
| 389 | \begin{funcdesc}{isSequenceType}{o}
|
|---|
| 390 | Returns true if the object \var{o} supports the sequence protocol.
|
|---|
| 391 | This returns true for all objects which define sequence methods in C,
|
|---|
| 392 | and for all instance objects defining \method{__getitem__}.
|
|---|
| 393 | \warning{There is no reliable
|
|---|
| 394 | way to test if an instance supports the complete sequence interface
|
|---|
| 395 | since the interface itself is ill-defined. This makes this test less
|
|---|
| 396 | useful than it otherwise might be.}
|
|---|
| 397 | \end{funcdesc}
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 | Example: Build a dictionary that maps the ordinals from \code{0} to
|
|---|
| 401 | \code{255} to their character equivalents.
|
|---|
| 402 |
|
|---|
| 403 | \begin{verbatim}
|
|---|
| 404 | >>> import operator
|
|---|
| 405 | >>> d = {}
|
|---|
| 406 | >>> keys = range(256)
|
|---|
| 407 | >>> vals = map(chr, keys)
|
|---|
| 408 | >>> map(operator.setitem, [d]*len(keys), keys, vals)
|
|---|
| 409 | \end{verbatim}
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 | The \module{operator} module also defines tools for generalized attribute
|
|---|
| 413 | and item lookups. These are useful for making fast field extractors
|
|---|
| 414 | as arguments for \function{map()}, \function{sorted()},
|
|---|
| 415 | \method{itertools.groupby()}, or other functions that expect a
|
|---|
| 416 | function argument.
|
|---|
| 417 |
|
|---|
| 418 | \begin{funcdesc}{attrgetter}{attr\optional{, args...}}
|
|---|
| 419 | Return a callable object that fetches \var{attr} from its operand.
|
|---|
| 420 | If more than one attribute is requested, returns a tuple of attributes.
|
|---|
| 421 | After, \samp{f=attrgetter('name')}, the call \samp{f(b)} returns
|
|---|
| 422 | \samp{b.name}. After, \samp{f=attrgetter('name', 'date')}, the call
|
|---|
| 423 | \samp{f(b)} returns \samp{(b.name, b.date)}.
|
|---|
| 424 | \versionadded{2.4}
|
|---|
| 425 | \versionchanged[Added support for multiple attributes]{2.5}
|
|---|
| 426 | \end{funcdesc}
|
|---|
| 427 |
|
|---|
| 428 | \begin{funcdesc}{itemgetter}{item\optional{, args...}}
|
|---|
| 429 | Return a callable object that fetches \var{item} from its operand.
|
|---|
| 430 | If more than one item is requested, returns a tuple of items.
|
|---|
| 431 | After, \samp{f=itemgetter(2)}, the call \samp{f(b)} returns
|
|---|
| 432 | \samp{b[2]}.
|
|---|
| 433 | After, \samp{f=itemgetter(2,5,3)}, the call \samp{f(b)} returns
|
|---|
| 434 | \samp{(b[2], b[5], b[3])}.
|
|---|
| 435 | \versionadded{2.4}
|
|---|
| 436 | \versionchanged[Added support for multiple item extraction]{2.5}
|
|---|
| 437 | \end{funcdesc}
|
|---|
| 438 |
|
|---|
| 439 | Examples:
|
|---|
| 440 |
|
|---|
| 441 | \begin{verbatim}
|
|---|
| 442 | >>> from operator import itemgetter
|
|---|
| 443 | >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
|
|---|
| 444 | >>> getcount = itemgetter(1)
|
|---|
| 445 | >>> map(getcount, inventory)
|
|---|
| 446 | [3, 2, 5, 1]
|
|---|
| 447 | >>> sorted(inventory, key=getcount)
|
|---|
| 448 | [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]
|
|---|
| 449 | \end{verbatim}
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 | \subsection{Mapping Operators to Functions \label{operator-map}}
|
|---|
| 453 |
|
|---|
| 454 | This table shows how abstract operations correspond to operator
|
|---|
| 455 | symbols in the Python syntax and the functions in the
|
|---|
| 456 | \refmodule{operator} module.
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 | \begin{tableiii}{l|c|l}{textrm}{Operation}{Syntax}{Function}
|
|---|
| 460 | \lineiii{Addition}{\code{\var{a} + \var{b}}}
|
|---|
| 461 | {\code{add(\var{a}, \var{b})}}
|
|---|
| 462 | \lineiii{Concatenation}{\code{\var{seq1} + \var{seq2}}}
|
|---|
| 463 | {\code{concat(\var{seq1}, \var{seq2})}}
|
|---|
| 464 | \lineiii{Containment Test}{\code{\var{o} in \var{seq}}}
|
|---|
| 465 | {\code{contains(\var{seq}, \var{o})}}
|
|---|
| 466 | \lineiii{Division}{\code{\var{a} / \var{b}}}
|
|---|
| 467 | {\code{div(\var{a}, \var{b}) \#} without \code{__future__.division}}
|
|---|
| 468 | \lineiii{Division}{\code{\var{a} / \var{b}}}
|
|---|
| 469 | {\code{truediv(\var{a}, \var{b}) \#} with \code{__future__.division}}
|
|---|
| 470 | \lineiii{Division}{\code{\var{a} // \var{b}}}
|
|---|
| 471 | {\code{floordiv(\var{a}, \var{b})}}
|
|---|
| 472 | \lineiii{Bitwise And}{\code{\var{a} \&\ \var{b}}}
|
|---|
| 473 | {\code{and_(\var{a}, \var{b})}}
|
|---|
| 474 | \lineiii{Bitwise Exclusive Or}{\code{\var{a} \^\ \var{b}}}
|
|---|
| 475 | {\code{xor(\var{a}, \var{b})}}
|
|---|
| 476 | \lineiii{Bitwise Inversion}{\code{\~{} \var{a}}}
|
|---|
| 477 | {\code{invert(\var{a})}}
|
|---|
| 478 | \lineiii{Bitwise Or}{\code{\var{a} | \var{b}}}
|
|---|
| 479 | {\code{or_(\var{a}, \var{b})}}
|
|---|
| 480 | \lineiii{Exponentiation}{\code{\var{a} ** \var{b}}}
|
|---|
| 481 | {\code{pow(\var{a}, \var{b})}}
|
|---|
| 482 | \lineiii{Identity}{\code{\var{a} is \var{b}}}
|
|---|
| 483 | {\code{is_(\var{a}, \var{b})}}
|
|---|
| 484 | \lineiii{Identity}{\code{\var{a} is not \var{b}}}
|
|---|
| 485 | {\code{is_not(\var{a}, \var{b})}}
|
|---|
| 486 | \lineiii{Indexed Assignment}{\code{\var{o}[\var{k}] = \var{v}}}
|
|---|
| 487 | {\code{setitem(\var{o}, \var{k}, \var{v})}}
|
|---|
| 488 | \lineiii{Indexed Deletion}{\code{del \var{o}[\var{k}]}}
|
|---|
| 489 | {\code{delitem(\var{o}, \var{k})}}
|
|---|
| 490 | \lineiii{Indexing}{\code{\var{o}[\var{k}]}}
|
|---|
| 491 | {\code{getitem(\var{o}, \var{k})}}
|
|---|
| 492 | \lineiii{Left Shift}{\code{\var{a} <\code{<} \var{b}}}
|
|---|
| 493 | {\code{lshift(\var{a}, \var{b})}}
|
|---|
| 494 | \lineiii{Modulo}{\code{\var{a} \%\ \var{b}}}
|
|---|
| 495 | {\code{mod(\var{a}, \var{b})}}
|
|---|
| 496 | \lineiii{Multiplication}{\code{\var{a} * \var{b}}}
|
|---|
| 497 | {\code{mul(\var{a}, \var{b})}}
|
|---|
| 498 | \lineiii{Negation (Arithmetic)}{\code{- \var{a}}}
|
|---|
| 499 | {\code{neg(\var{a})}}
|
|---|
| 500 | \lineiii{Negation (Logical)}{\code{not \var{a}}}
|
|---|
| 501 | {\code{not_(\var{a})}}
|
|---|
| 502 | \lineiii{Right Shift}{\code{\var{a} >> \var{b}}}
|
|---|
| 503 | {\code{rshift(\var{a}, \var{b})}}
|
|---|
| 504 | \lineiii{Sequence Repitition}{\code{\var{seq} * \var{i}}}
|
|---|
| 505 | {\code{repeat(\var{seq}, \var{i})}}
|
|---|
| 506 | \lineiii{Slice Assignment}{\code{\var{seq}[\var{i}:\var{j}]} = \var{values}}
|
|---|
| 507 | {\code{setslice(\var{seq}, \var{i}, \var{j}, \var{values})}}
|
|---|
| 508 | \lineiii{Slice Deletion}{\code{del \var{seq}[\var{i}:\var{j}]}}
|
|---|
| 509 | {\code{delslice(\var{seq}, \var{i}, \var{j})}}
|
|---|
| 510 | \lineiii{Slicing}{\code{\var{seq}[\var{i}:\var{j}]}}
|
|---|
| 511 | {\code{getslice(\var{seq}, \var{i}, \var{j})}}
|
|---|
| 512 | \lineiii{String Formatting}{\code{\var{s} \%\ \var{o}}}
|
|---|
| 513 | {\code{mod(\var{s}, \var{o})}}
|
|---|
| 514 | \lineiii{Subtraction}{\code{\var{a} - \var{b}}}
|
|---|
| 515 | {\code{sub(\var{a}, \var{b})}}
|
|---|
| 516 | \lineiii{Truth Test}{\code{\var{o}}}
|
|---|
| 517 | {\code{truth(\var{o})}}
|
|---|
| 518 | \lineiii{Ordering}{\code{\var{a} < \var{b}}}
|
|---|
| 519 | {\code{lt(\var{a}, \var{b})}}
|
|---|
| 520 | \lineiii{Ordering}{\code{\var{a} <= \var{b}}}
|
|---|
| 521 | {\code{le(\var{a}, \var{b})}}
|
|---|
| 522 | \lineiii{Equality}{\code{\var{a} == \var{b}}}
|
|---|
| 523 | {\code{eq(\var{a}, \var{b})}}
|
|---|
| 524 | \lineiii{Difference}{\code{\var{a} != \var{b}}}
|
|---|
| 525 | {\code{ne(\var{a}, \var{b})}}
|
|---|
| 526 | \lineiii{Ordering}{\code{\var{a} >= \var{b}}}
|
|---|
| 527 | {\code{ge(\var{a}, \var{b})}}
|
|---|
| 528 | \lineiii{Ordering}{\code{\var{a} > \var{b}}}
|
|---|
| 529 | {\code{gt(\var{a}, \var{b})}}
|
|---|
| 530 | \end{tableiii}
|
|---|