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