Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :mod:`operator` --- Standard operators as functions |
| 2 | =================================================== |
| 3 | |
| 4 | .. module:: operator |
| 5 | :synopsis: Functions corresponding to the standard operators. |
| 6 | .. sectionauthor:: Skip Montanaro <skip@automatrix.com> |
| 7 | |
| 8 | |
| 9 | |
| 10 | The :mod:`operator` module exports a set of functions implemented in C |
| 11 | corresponding to the intrinsic operators of Python. For example, |
| 12 | ``operator.add(x, y)`` is equivalent to the expression ``x+y``. The function |
| 13 | names are those used for special class methods; variants without leading and |
| 14 | trailing ``__`` are also provided for convenience. |
| 15 | |
| 16 | The functions fall into categories that perform object comparisons, logical |
| 17 | operations, mathematical operations, sequence operations, and abstract type |
| 18 | tests. |
| 19 | |
| 20 | The object comparison functions are useful for all objects, and are named after |
| 21 | the rich comparison operators they support: |
| 22 | |
| 23 | |
| 24 | .. function:: lt(a, b) |
| 25 | le(a, b) |
| 26 | eq(a, b) |
| 27 | ne(a, b) |
| 28 | ge(a, b) |
| 29 | gt(a, b) |
| 30 | __lt__(a, b) |
| 31 | __le__(a, b) |
| 32 | __eq__(a, b) |
| 33 | __ne__(a, b) |
| 34 | __ge__(a, b) |
| 35 | __gt__(a, b) |
| 36 | |
| 37 | Perform "rich comparisons" between *a* and *b*. Specifically, ``lt(a, b)`` is |
| 38 | equivalent to ``a < b``, ``le(a, b)`` is equivalent to ``a <= b``, ``eq(a, |
| 39 | b)`` is equivalent to ``a == b``, ``ne(a, b)`` is equivalent to ``a != b``, |
| 40 | ``gt(a, b)`` is equivalent to ``a > b`` and ``ge(a, b)`` is equivalent to ``a |
| 41 | >= b``. Note that unlike the built-in :func:`cmp`, these functions can |
| 42 | return any value, which may or may not be interpretable as a Boolean value. |
| 43 | See :ref:`comparisons` for more information about rich comparisons. |
| 44 | |
| 45 | .. versionadded:: 2.2 |
| 46 | |
| 47 | The logical operations are also generally applicable to all objects, and support |
| 48 | truth tests, identity tests, and boolean operations: |
| 49 | |
| 50 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 51 | .. function:: not_(obj) |
| 52 | __not__(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 53 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 54 | Return the outcome of :keyword:`not` *obj*. (Note that there is no |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 55 | :meth:`__not__` method for object instances; only the interpreter core defines |
| 56 | this operation. The result is affected by the :meth:`__nonzero__` and |
| 57 | :meth:`__len__` methods.) |
| 58 | |
| 59 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 60 | .. function:: truth(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 61 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 62 | Return :const:`True` if *obj* is true, and :const:`False` otherwise. This is |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 63 | equivalent to using the :class:`bool` constructor. |
| 64 | |
| 65 | |
| 66 | .. function:: is_(a, b) |
| 67 | |
| 68 | Return ``a is b``. Tests object identity. |
| 69 | |
| 70 | .. versionadded:: 2.3 |
| 71 | |
| 72 | |
| 73 | .. function:: is_not(a, b) |
| 74 | |
| 75 | Return ``a is not b``. Tests object identity. |
| 76 | |
| 77 | .. versionadded:: 2.3 |
| 78 | |
| 79 | The mathematical and bitwise operations are the most numerous: |
| 80 | |
| 81 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 82 | .. function:: abs(obj) |
| 83 | __abs__(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 84 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 85 | Return the absolute value of *obj*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | .. function:: add(a, b) |
| 89 | __add__(a, b) |
| 90 | |
| 91 | Return ``a + b``, for *a* and *b* numbers. |
| 92 | |
| 93 | |
| 94 | .. function:: and_(a, b) |
| 95 | __and__(a, b) |
| 96 | |
| 97 | Return the bitwise and of *a* and *b*. |
| 98 | |
| 99 | |
| 100 | .. function:: div(a, b) |
| 101 | __div__(a, b) |
| 102 | |
| 103 | Return ``a / b`` when ``__future__.division`` is not in effect. This is |
| 104 | also known as "classic" division. |
| 105 | |
| 106 | |
| 107 | .. function:: floordiv(a, b) |
| 108 | __floordiv__(a, b) |
| 109 | |
| 110 | Return ``a // b``. |
| 111 | |
| 112 | .. versionadded:: 2.2 |
| 113 | |
| 114 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 115 | .. function:: inv(obj) |
| 116 | invert(obj) |
| 117 | __inv__(obj) |
| 118 | __invert__(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 119 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 120 | Return the bitwise inverse of the number *obj*. This is equivalent to ``~obj``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 121 | |
| 122 | .. versionadded:: 2.0 |
| 123 | The names :func:`invert` and :func:`__invert__`. |
| 124 | |
| 125 | |
| 126 | .. function:: lshift(a, b) |
| 127 | __lshift__(a, b) |
| 128 | |
| 129 | Return *a* shifted left by *b*. |
| 130 | |
| 131 | |
| 132 | .. function:: mod(a, b) |
| 133 | __mod__(a, b) |
| 134 | |
| 135 | Return ``a % b``. |
| 136 | |
| 137 | |
| 138 | .. function:: mul(a, b) |
| 139 | __mul__(a, b) |
| 140 | |
| 141 | Return ``a * b``, for *a* and *b* numbers. |
| 142 | |
| 143 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 144 | .. function:: neg(obj) |
| 145 | __neg__(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 146 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 147 | Return *obj* negated. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 148 | |
| 149 | |
| 150 | .. function:: or_(a, b) |
| 151 | __or__(a, b) |
| 152 | |
| 153 | Return the bitwise or of *a* and *b*. |
| 154 | |
| 155 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 156 | .. function:: pos(obj) |
| 157 | __pos__(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 158 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 159 | Return *obj* positive. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 160 | |
| 161 | |
| 162 | .. function:: pow(a, b) |
| 163 | __pow__(a, b) |
| 164 | |
| 165 | Return ``a ** b``, for *a* and *b* numbers. |
| 166 | |
| 167 | .. versionadded:: 2.3 |
| 168 | |
| 169 | |
| 170 | .. function:: rshift(a, b) |
| 171 | __rshift__(a, b) |
| 172 | |
| 173 | Return *a* shifted right by *b*. |
| 174 | |
| 175 | |
| 176 | .. function:: sub(a, b) |
| 177 | __sub__(a, b) |
| 178 | |
| 179 | Return ``a - b``. |
| 180 | |
| 181 | |
| 182 | .. function:: truediv(a, b) |
| 183 | __truediv__(a, b) |
| 184 | |
| 185 | Return ``a / b`` when ``__future__.division`` is in effect. This is also |
| 186 | known as "true" division. |
| 187 | |
| 188 | .. versionadded:: 2.2 |
| 189 | |
| 190 | |
| 191 | .. function:: xor(a, b) |
| 192 | __xor__(a, b) |
| 193 | |
| 194 | Return the bitwise exclusive or of *a* and *b*. |
| 195 | |
| 196 | |
| 197 | .. function:: index(a) |
| 198 | __index__(a) |
| 199 | |
| 200 | Return *a* converted to an integer. Equivalent to ``a.__index__()``. |
| 201 | |
| 202 | .. versionadded:: 2.5 |
| 203 | |
| 204 | |
| 205 | Operations which work with sequences include: |
| 206 | |
| 207 | .. function:: concat(a, b) |
| 208 | __concat__(a, b) |
| 209 | |
| 210 | Return ``a + b`` for *a* and *b* sequences. |
| 211 | |
| 212 | |
| 213 | .. function:: contains(a, b) |
| 214 | __contains__(a, b) |
| 215 | |
| 216 | Return the outcome of the test ``b in a``. Note the reversed operands. |
| 217 | |
| 218 | .. versionadded:: 2.0 |
| 219 | The name :func:`__contains__`. |
| 220 | |
| 221 | |
| 222 | .. function:: countOf(a, b) |
| 223 | |
| 224 | Return the number of occurrences of *b* in *a*. |
| 225 | |
| 226 | |
| 227 | .. function:: delitem(a, b) |
| 228 | __delitem__(a, b) |
| 229 | |
| 230 | Remove the value of *a* at index *b*. |
| 231 | |
| 232 | |
| 233 | .. function:: delslice(a, b, c) |
| 234 | __delslice__(a, b, c) |
| 235 | |
| 236 | Delete the slice of *a* from index *b* to index *c-1*. |
| 237 | |
| 238 | |
| 239 | .. function:: getitem(a, b) |
| 240 | __getitem__(a, b) |
| 241 | |
| 242 | Return the value of *a* at index *b*. |
| 243 | |
| 244 | |
| 245 | .. function:: getslice(a, b, c) |
| 246 | __getslice__(a, b, c) |
| 247 | |
| 248 | Return the slice of *a* from index *b* to index *c-1*. |
| 249 | |
| 250 | |
| 251 | .. function:: indexOf(a, b) |
| 252 | |
| 253 | Return the index of the first of occurrence of *b* in *a*. |
| 254 | |
| 255 | |
| 256 | .. function:: repeat(a, b) |
| 257 | __repeat__(a, b) |
| 258 | |
| 259 | Return ``a * b`` where *a* is a sequence and *b* is an integer. |
| 260 | |
| 261 | |
| 262 | .. function:: sequenceIncludes(...) |
| 263 | |
| 264 | .. deprecated:: 2.0 |
| 265 | Use :func:`contains` instead. |
| 266 | |
| 267 | Alias for :func:`contains`. |
| 268 | |
| 269 | |
| 270 | .. function:: setitem(a, b, c) |
| 271 | __setitem__(a, b, c) |
| 272 | |
| 273 | Set the value of *a* at index *b* to *c*. |
| 274 | |
| 275 | |
| 276 | .. function:: setslice(a, b, c, v) |
| 277 | __setslice__(a, b, c, v) |
| 278 | |
| 279 | Set the slice of *a* from index *b* to index *c-1* to the sequence *v*. |
| 280 | |
| 281 | Many operations have an "in-place" version. The following functions provide a |
| 282 | more primitive access to in-place operators than the usual syntax does; for |
Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 283 | example, the :term:`statement` ``x += y`` is equivalent to |
| 284 | ``x = operator.iadd(x, y)``. Another way to put it is to say that |
| 285 | ``z = operator.iadd(x, y)`` is equivalent to the compound statement |
| 286 | ``z = x; z += y``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 287 | |
| 288 | .. function:: iadd(a, b) |
| 289 | __iadd__(a, b) |
| 290 | |
| 291 | ``a = iadd(a, b)`` is equivalent to ``a += b``. |
| 292 | |
| 293 | .. versionadded:: 2.5 |
| 294 | |
| 295 | |
| 296 | .. function:: iand(a, b) |
| 297 | __iand__(a, b) |
| 298 | |
| 299 | ``a = iand(a, b)`` is equivalent to ``a &= b``. |
| 300 | |
| 301 | .. versionadded:: 2.5 |
| 302 | |
| 303 | |
| 304 | .. function:: iconcat(a, b) |
| 305 | __iconcat__(a, b) |
| 306 | |
| 307 | ``a = iconcat(a, b)`` is equivalent to ``a += b`` for *a* and *b* sequences. |
| 308 | |
| 309 | .. versionadded:: 2.5 |
| 310 | |
| 311 | |
| 312 | .. function:: idiv(a, b) |
| 313 | __idiv__(a, b) |
| 314 | |
| 315 | ``a = idiv(a, b)`` is equivalent to ``a /= b`` when ``__future__.division`` is |
| 316 | not in effect. |
| 317 | |
| 318 | .. versionadded:: 2.5 |
| 319 | |
| 320 | |
| 321 | .. function:: ifloordiv(a, b) |
| 322 | __ifloordiv__(a, b) |
| 323 | |
| 324 | ``a = ifloordiv(a, b)`` is equivalent to ``a //= b``. |
| 325 | |
| 326 | .. versionadded:: 2.5 |
| 327 | |
| 328 | |
| 329 | .. function:: ilshift(a, b) |
| 330 | __ilshift__(a, b) |
| 331 | |
| 332 | ``a = ilshift(a, b)`` is equivalent to ``a <``\ ``<= b``. |
| 333 | |
| 334 | .. versionadded:: 2.5 |
| 335 | |
| 336 | |
| 337 | .. function:: imod(a, b) |
| 338 | __imod__(a, b) |
| 339 | |
| 340 | ``a = imod(a, b)`` is equivalent to ``a %= b``. |
| 341 | |
| 342 | .. versionadded:: 2.5 |
| 343 | |
| 344 | |
| 345 | .. function:: imul(a, b) |
| 346 | __imul__(a, b) |
| 347 | |
| 348 | ``a = imul(a, b)`` is equivalent to ``a *= b``. |
| 349 | |
| 350 | .. versionadded:: 2.5 |
| 351 | |
| 352 | |
| 353 | .. function:: ior(a, b) |
| 354 | __ior__(a, b) |
| 355 | |
| 356 | ``a = ior(a, b)`` is equivalent to ``a |= b``. |
| 357 | |
| 358 | .. versionadded:: 2.5 |
| 359 | |
| 360 | |
| 361 | .. function:: ipow(a, b) |
| 362 | __ipow__(a, b) |
| 363 | |
| 364 | ``a = ipow(a, b)`` is equivalent to ``a **= b``. |
| 365 | |
| 366 | .. versionadded:: 2.5 |
| 367 | |
| 368 | |
| 369 | .. function:: irepeat(a, b) |
| 370 | __irepeat__(a, b) |
| 371 | |
| 372 | ``a = irepeat(a, b)`` is equivalent to ``a *= b`` where *a* is a sequence and |
| 373 | *b* is an integer. |
| 374 | |
| 375 | .. versionadded:: 2.5 |
| 376 | |
| 377 | |
| 378 | .. function:: irshift(a, b) |
| 379 | __irshift__(a, b) |
| 380 | |
| 381 | ``a = irshift(a, b)`` is equivalent to ``a >>= b``. |
| 382 | |
| 383 | .. versionadded:: 2.5 |
| 384 | |
| 385 | |
| 386 | .. function:: isub(a, b) |
| 387 | __isub__(a, b) |
| 388 | |
| 389 | ``a = isub(a, b)`` is equivalent to ``a -= b``. |
| 390 | |
| 391 | .. versionadded:: 2.5 |
| 392 | |
| 393 | |
| 394 | .. function:: itruediv(a, b) |
| 395 | __itruediv__(a, b) |
| 396 | |
| 397 | ``a = itruediv(a, b)`` is equivalent to ``a /= b`` when ``__future__.division`` |
| 398 | is in effect. |
| 399 | |
| 400 | .. versionadded:: 2.5 |
| 401 | |
| 402 | |
| 403 | .. function:: ixor(a, b) |
| 404 | __ixor__(a, b) |
| 405 | |
| 406 | ``a = ixor(a, b)`` is equivalent to ``a ^= b``. |
| 407 | |
| 408 | .. versionadded:: 2.5 |
| 409 | |
| 410 | |
| 411 | The :mod:`operator` module also defines a few predicates to test the type of |
| 412 | objects. |
| 413 | |
| 414 | .. note:: |
| 415 | |
| 416 | Be careful not to misinterpret the results of these functions; only |
| 417 | :func:`isCallable` has any measure of reliability with instance objects. |
Georg Brandl | 4f0f34f | 2008-03-22 21:26:44 +0000 | [diff] [blame] | 418 | For example: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 419 | |
| 420 | >>> class C: |
| 421 | ... pass |
| 422 | ... |
| 423 | >>> import operator |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 424 | >>> obj = C() |
| 425 | >>> operator.isMappingType(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 426 | True |
| 427 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 428 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 429 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 430 | Python 3 is expected to introduce abstract base classes for |
| 431 | collection types, so it should be possible to write, for example, |
| 432 | ``isinstance(obj, collections.Mapping)`` and ``isinstance(obj, |
| 433 | collections.Sequence)``. |
| 434 | |
| 435 | .. function:: isCallable(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 436 | |
| 437 | .. deprecated:: 2.0 |
| 438 | Use the :func:`callable` built-in function instead. |
| 439 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 440 | Returns true if the object *obj* can be called like a function, otherwise it |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 441 | returns false. True is returned for functions, bound and unbound methods, class |
| 442 | objects, and instance objects which support the :meth:`__call__` method. |
| 443 | |
| 444 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 445 | .. function:: isMappingType(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 446 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 447 | Returns true if the object *obj* supports the mapping interface. This is true for |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 448 | dictionaries and all instance objects defining :meth:`__getitem__`. |
| 449 | |
| 450 | .. warning:: |
| 451 | |
| 452 | There is no reliable way to test if an instance supports the complete mapping |
| 453 | protocol since the interface itself is ill-defined. This makes this test less |
| 454 | useful than it otherwise might be. |
| 455 | |
| 456 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 457 | .. function:: isNumberType(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 458 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 459 | Returns true if the object *obj* represents a number. This is true for all |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 460 | numeric types implemented in C. |
| 461 | |
| 462 | .. warning:: |
| 463 | |
| 464 | There is no reliable way to test if an instance supports the complete numeric |
| 465 | interface since the interface itself is ill-defined. This makes this test less |
| 466 | useful than it otherwise might be. |
| 467 | |
| 468 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 469 | .. function:: isSequenceType(obj) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 470 | |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 471 | Returns true if the object *obj* supports the sequence protocol. This returns true |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 472 | for all objects which define sequence methods in C, and for all instance objects |
| 473 | defining :meth:`__getitem__`. |
| 474 | |
| 475 | .. warning:: |
| 476 | |
| 477 | There is no reliable way to test if an instance supports the complete sequence |
| 478 | interface since the interface itself is ill-defined. This makes this test less |
| 479 | useful than it otherwise might be. |
| 480 | |
| 481 | Example: Build a dictionary that maps the ordinals from ``0`` to ``255`` to |
Georg Brandl | 4f0f34f | 2008-03-22 21:26:44 +0000 | [diff] [blame] | 482 | their character equivalents. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 483 | |
| 484 | >>> import operator |
| 485 | >>> d = {} |
| 486 | >>> keys = range(256) |
| 487 | >>> vals = map(chr, keys) |
| 488 | >>> map(operator.setitem, [d]*len(keys), keys, vals) |
Georg Brandl | 4f0f34f | 2008-03-22 21:26:44 +0000 | [diff] [blame] | 489 | [None, None, ..., None] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 490 | |
| 491 | .. XXX: find a better, readable, example |
| 492 | |
| 493 | The :mod:`operator` module also defines tools for generalized attribute and item |
| 494 | lookups. These are useful for making fast field extractors as arguments for |
| 495 | :func:`map`, :func:`sorted`, :meth:`itertools.groupby`, or other functions that |
| 496 | expect a function argument. |
| 497 | |
| 498 | |
| 499 | .. function:: attrgetter(attr[, args...]) |
| 500 | |
| 501 | Return a callable object that fetches *attr* from its operand. If more than one |
| 502 | attribute is requested, returns a tuple of attributes. After, |
Georg Brandl | e2065c6 | 2008-02-23 23:02:23 +0000 | [diff] [blame] | 503 | ``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After, |
| 504 | ``f = attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name, |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 505 | b.date)``. |
| 506 | |
Georg Brandl | e2065c6 | 2008-02-23 23:02:23 +0000 | [diff] [blame] | 507 | The attribute names can also contain dots; after ``f = attrgetter('date.month')``, |
| 508 | the call ``f(b)`` returns ``b.date.month``. |
| 509 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 510 | .. versionadded:: 2.4 |
| 511 | |
| 512 | .. versionchanged:: 2.5 |
| 513 | Added support for multiple attributes. |
| 514 | |
Georg Brandl | e2065c6 | 2008-02-23 23:02:23 +0000 | [diff] [blame] | 515 | .. versionchanged:: 2.6 |
| 516 | Added support for dotted attributes. |
| 517 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 518 | |
| 519 | .. function:: itemgetter(item[, args...]) |
| 520 | |
Raymond Hettinger | 513460f | 2008-03-11 21:37:46 +0000 | [diff] [blame] | 521 | Return a callable object that fetches *item* from its operand using the |
| 522 | operand's :meth:`__getitem__` method. If multiple items are specified, |
| 523 | returns a tuple of lookup values. Equivalent to:: |
| 524 | |
| 525 | def itemgetter(*items): |
| 526 | if len(items) == 1: |
| 527 | item = items[0] |
| 528 | def g(obj): |
| 529 | return obj[item] |
| 530 | else: |
| 531 | def g(obj): |
| 532 | return tuple(obj[item] for item in items) |
| 533 | return g |
| 534 | |
| 535 | The items can be any type accepted by the operand's :meth:`__getitem__` |
| 536 | method. Dictionaries accept any hashable value. Lists, tuples, and |
Georg Brandl | 4f0f34f | 2008-03-22 21:26:44 +0000 | [diff] [blame] | 537 | strings accept an index or a slice: |
Raymond Hettinger | 513460f | 2008-03-11 21:37:46 +0000 | [diff] [blame] | 538 | |
Georg Brandl | 4f0f34f | 2008-03-22 21:26:44 +0000 | [diff] [blame] | 539 | >>> from operator import itemgetter |
| 540 | >>> itemgetter(1)('ABCDEFG') |
| 541 | 'B' |
| 542 | >>> itemgetter(1,3,5)('ABCDEFG') |
| 543 | ('B', 'D', 'F') |
| 544 | >>> itemgetter(slice(2,None))('ABCDEFG') |
| 545 | 'CDEFG' |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 546 | |
| 547 | .. versionadded:: 2.4 |
| 548 | |
| 549 | .. versionchanged:: 2.5 |
| 550 | Added support for multiple item extraction. |
| 551 | |
Raymond Hettinger | 513460f | 2008-03-11 21:37:46 +0000 | [diff] [blame] | 552 | Example of using :func:`itemgetter` to retrieve specific fields from a |
Georg Brandl | 4f0f34f | 2008-03-22 21:26:44 +0000 | [diff] [blame] | 553 | tuple record: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 554 | |
Raymond Hettinger | 513460f | 2008-03-11 21:37:46 +0000 | [diff] [blame] | 555 | >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] |
| 556 | >>> getcount = itemgetter(1) |
| 557 | >>> map(getcount, inventory) |
| 558 | [3, 2, 5, 1] |
| 559 | >>> sorted(inventory, key=getcount) |
| 560 | [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 561 | |
| 562 | |
Georg Brandl | ebcfd11 | 2008-02-23 23:04:35 +0000 | [diff] [blame] | 563 | .. function:: methodcaller(name[, args...]) |
| 564 | |
| 565 | Return a callable object that calls the method *name* on its operand. If |
| 566 | additional arguments and/or keyword arguments are given, they will be given |
| 567 | to the method as well. After ``f = methodcaller('name')``, the call ``f(b)`` |
| 568 | returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the |
| 569 | call ``f(b)`` returns ``b.name('foo', bar=1)``. |
| 570 | |
| 571 | .. versionadded:: 2.6 |
| 572 | |
| 573 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 574 | .. _operator-map: |
| 575 | |
| 576 | Mapping Operators to Functions |
| 577 | ------------------------------ |
| 578 | |
| 579 | This table shows how abstract operations correspond to operator symbols in the |
| 580 | Python syntax and the functions in the :mod:`operator` module. |
| 581 | |
| 582 | +-----------------------+-------------------------+---------------------------------+ |
| 583 | | Operation | Syntax | Function | |
| 584 | +=======================+=========================+=================================+ |
| 585 | | Addition | ``a + b`` | ``add(a, b)`` | |
| 586 | +-----------------------+-------------------------+---------------------------------+ |
| 587 | | Concatenation | ``seq1 + seq2`` | ``concat(seq1, seq2)`` | |
| 588 | +-----------------------+-------------------------+---------------------------------+ |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 589 | | Containment Test | ``obj in seq`` | ``contains(seq, obj)`` | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 590 | +-----------------------+-------------------------+---------------------------------+ |
| 591 | | Division | ``a / b`` | ``div(a, b)`` (without | |
| 592 | | | | ``__future__.division``) | |
| 593 | +-----------------------+-------------------------+---------------------------------+ |
| 594 | | Division | ``a / b`` | ``truediv(a, b)`` (with | |
| 595 | | | | ``__future__.division``) | |
| 596 | +-----------------------+-------------------------+---------------------------------+ |
| 597 | | Division | ``a // b`` | ``floordiv(a, b)`` | |
| 598 | +-----------------------+-------------------------+---------------------------------+ |
| 599 | | Bitwise And | ``a & b`` | ``and_(a, b)`` | |
| 600 | +-----------------------+-------------------------+---------------------------------+ |
| 601 | | Bitwise Exclusive Or | ``a ^ b`` | ``xor(a, b)`` | |
| 602 | +-----------------------+-------------------------+---------------------------------+ |
| 603 | | Bitwise Inversion | ``~ a`` | ``invert(a)`` | |
| 604 | +-----------------------+-------------------------+---------------------------------+ |
| 605 | | Bitwise Or | ``a | b`` | ``or_(a, b)`` | |
| 606 | +-----------------------+-------------------------+---------------------------------+ |
| 607 | | Exponentiation | ``a ** b`` | ``pow(a, b)`` | |
| 608 | +-----------------------+-------------------------+---------------------------------+ |
| 609 | | Identity | ``a is b`` | ``is_(a, b)`` | |
| 610 | +-----------------------+-------------------------+---------------------------------+ |
| 611 | | Identity | ``a is not b`` | ``is_not(a, b)`` | |
| 612 | +-----------------------+-------------------------+---------------------------------+ |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 613 | | Indexed Assignment | ``obj[k] = v`` | ``setitem(obj, k, v)`` | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 614 | +-----------------------+-------------------------+---------------------------------+ |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 615 | | Indexed Deletion | ``del obj[k]`` | ``delitem(obj, k)`` | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 616 | +-----------------------+-------------------------+---------------------------------+ |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 617 | | Indexing | ``obj[k]`` | ``getitem(obj, k)`` | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 618 | +-----------------------+-------------------------+---------------------------------+ |
| 619 | | Left Shift | ``a << b`` | ``lshift(a, b)`` | |
| 620 | +-----------------------+-------------------------+---------------------------------+ |
| 621 | | Modulo | ``a % b`` | ``mod(a, b)`` | |
| 622 | +-----------------------+-------------------------+---------------------------------+ |
| 623 | | Multiplication | ``a * b`` | ``mul(a, b)`` | |
| 624 | +-----------------------+-------------------------+---------------------------------+ |
| 625 | | Negation (Arithmetic) | ``- a`` | ``neg(a)`` | |
| 626 | +-----------------------+-------------------------+---------------------------------+ |
| 627 | | Negation (Logical) | ``not a`` | ``not_(a)`` | |
| 628 | +-----------------------+-------------------------+---------------------------------+ |
| 629 | | Right Shift | ``a >> b`` | ``rshift(a, b)`` | |
| 630 | +-----------------------+-------------------------+---------------------------------+ |
| 631 | | Sequence Repitition | ``seq * i`` | ``repeat(seq, i)`` | |
| 632 | +-----------------------+-------------------------+---------------------------------+ |
| 633 | | Slice Assignment | ``seq[i:j] = values`` | ``setslice(seq, i, j, values)`` | |
| 634 | +-----------------------+-------------------------+---------------------------------+ |
| 635 | | Slice Deletion | ``del seq[i:j]`` | ``delslice(seq, i, j)`` | |
| 636 | +-----------------------+-------------------------+---------------------------------+ |
| 637 | | Slicing | ``seq[i:j]`` | ``getslice(seq, i, j)`` | |
| 638 | +-----------------------+-------------------------+---------------------------------+ |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 639 | | String Formatting | ``s % obj`` | ``mod(s, obj)`` | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 640 | +-----------------------+-------------------------+---------------------------------+ |
| 641 | | Subtraction | ``a - b`` | ``sub(a, b)`` | |
| 642 | +-----------------------+-------------------------+---------------------------------+ |
Mark Summerfield | ddca9f0 | 2007-09-13 14:54:30 +0000 | [diff] [blame] | 643 | | Truth Test | ``obj`` | ``truth(obj)`` | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 644 | +-----------------------+-------------------------+---------------------------------+ |
| 645 | | Ordering | ``a < b`` | ``lt(a, b)`` | |
| 646 | +-----------------------+-------------------------+---------------------------------+ |
| 647 | | Ordering | ``a <= b`` | ``le(a, b)`` | |
| 648 | +-----------------------+-------------------------+---------------------------------+ |
| 649 | | Equality | ``a == b`` | ``eq(a, b)`` | |
| 650 | +-----------------------+-------------------------+---------------------------------+ |
| 651 | | Difference | ``a != b`` | ``ne(a, b)`` | |
| 652 | +-----------------------+-------------------------+---------------------------------+ |
| 653 | | Ordering | ``a >= b`` | ``ge(a, b)`` | |
| 654 | +-----------------------+-------------------------+---------------------------------+ |
| 655 | | Ordering | ``a > b`` | ``gt(a, b)`` | |
| 656 | +-----------------------+-------------------------+---------------------------------+ |
| 657 | |