Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 9 | .. testsetup:: |
| 10 | |
| 11 | import operator |
| 12 | from operator import itemgetter |
| 13 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
| 51 | The logical operations are also generally applicable to all objects, and support |
| 52 | truth tests, identity tests, and boolean operations: |
| 53 | |
| 54 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 55 | .. function:: not_(obj) |
| 56 | __not__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 58 | Return the outcome of :keyword:`not` *obj*. (Note that there is no |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | :meth:`__not__` method for object instances; only the interpreter core defines |
| 60 | this operation. The result is affected by the :meth:`__bool__` and |
| 61 | :meth:`__len__` methods.) |
| 62 | |
| 63 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 64 | .. function:: truth(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 66 | Return :const:`True` if *obj* is true, and :const:`False` otherwise. This is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | equivalent to using the :class:`bool` constructor. |
| 68 | |
| 69 | |
| 70 | .. function:: is_(a, b) |
| 71 | |
| 72 | Return ``a is b``. Tests object identity. |
| 73 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | .. function:: is_not(a, b) |
| 76 | |
| 77 | Return ``a is not b``. Tests object identity. |
| 78 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
| 80 | The mathematical and bitwise operations are the most numerous: |
| 81 | |
| 82 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 83 | .. function:: abs(obj) |
| 84 | __abs__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 86 | Return the absolute value of *obj*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | .. function:: add(a, b) |
| 90 | __add__(a, b) |
| 91 | |
| 92 | Return ``a + b``, for *a* and *b* numbers. |
| 93 | |
| 94 | |
| 95 | .. function:: and_(a, b) |
| 96 | __and__(a, b) |
| 97 | |
| 98 | Return the bitwise and of *a* and *b*. |
| 99 | |
| 100 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 | .. function:: floordiv(a, b) |
| 102 | __floordiv__(a, b) |
| 103 | |
| 104 | Return ``a // b``. |
| 105 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 107 | .. function:: inv(obj) |
| 108 | invert(obj) |
| 109 | __inv__(obj) |
| 110 | __invert__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 112 | Return the bitwise inverse of the number *obj*. This is equivalent to ``~obj``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | |
| 115 | .. function:: lshift(a, b) |
| 116 | __lshift__(a, b) |
| 117 | |
| 118 | Return *a* shifted left by *b*. |
| 119 | |
| 120 | |
| 121 | .. function:: mod(a, b) |
| 122 | __mod__(a, b) |
| 123 | |
| 124 | Return ``a % b``. |
| 125 | |
| 126 | |
| 127 | .. function:: mul(a, b) |
| 128 | __mul__(a, b) |
| 129 | |
| 130 | Return ``a * b``, for *a* and *b* numbers. |
| 131 | |
| 132 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 133 | .. function:: neg(obj) |
| 134 | __neg__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 136 | Return *obj* negated. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | .. function:: or_(a, b) |
| 140 | __or__(a, b) |
| 141 | |
| 142 | Return the bitwise or of *a* and *b*. |
| 143 | |
| 144 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 145 | .. function:: pos(obj) |
| 146 | __pos__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 147 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 148 | Return *obj* positive. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 149 | |
| 150 | |
| 151 | .. function:: pow(a, b) |
| 152 | __pow__(a, b) |
| 153 | |
| 154 | Return ``a ** b``, for *a* and *b* numbers. |
| 155 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | |
| 157 | .. function:: rshift(a, b) |
| 158 | __rshift__(a, b) |
| 159 | |
| 160 | Return *a* shifted right by *b*. |
| 161 | |
| 162 | |
| 163 | .. function:: sub(a, b) |
| 164 | __sub__(a, b) |
| 165 | |
| 166 | Return ``a - b``. |
| 167 | |
| 168 | |
| 169 | .. function:: truediv(a, b) |
| 170 | __truediv__(a, b) |
| 171 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 172 | Return ``a / b`` where 2/3 is .66 rather than 0. This is also known as |
| 173 | "true" division. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 175 | |
| 176 | .. function:: xor(a, b) |
| 177 | __xor__(a, b) |
| 178 | |
| 179 | Return the bitwise exclusive or of *a* and *b*. |
| 180 | |
| 181 | |
| 182 | .. function:: index(a) |
| 183 | __index__(a) |
| 184 | |
| 185 | Return *a* converted to an integer. Equivalent to ``a.__index__()``. |
| 186 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 187 | |
| 188 | Operations which work with sequences include: |
| 189 | |
| 190 | .. function:: concat(a, b) |
| 191 | __concat__(a, b) |
| 192 | |
| 193 | Return ``a + b`` for *a* and *b* sequences. |
| 194 | |
| 195 | |
| 196 | .. function:: contains(a, b) |
| 197 | __contains__(a, b) |
| 198 | |
| 199 | Return the outcome of the test ``b in a``. Note the reversed operands. |
| 200 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 201 | |
| 202 | .. function:: countOf(a, b) |
| 203 | |
| 204 | Return the number of occurrences of *b* in *a*. |
| 205 | |
| 206 | |
| 207 | .. function:: delitem(a, b) |
| 208 | __delitem__(a, b) |
| 209 | |
| 210 | Remove the value of *a* at index *b*. |
| 211 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 212 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 213 | .. function:: getitem(a, b) |
| 214 | __getitem__(a, b) |
| 215 | |
| 216 | Return the value of *a* at index *b*. |
| 217 | |
| 218 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 219 | .. function:: indexOf(a, b) |
| 220 | |
| 221 | Return the index of the first of occurrence of *b* in *a*. |
| 222 | |
| 223 | |
| 224 | .. function:: repeat(a, b) |
| 225 | __repeat__(a, b) |
| 226 | |
| 227 | Return ``a * b`` where *a* is a sequence and *b* is an integer. |
| 228 | |
| 229 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 230 | .. function:: setitem(a, b, c) |
| 231 | __setitem__(a, b, c) |
| 232 | |
| 233 | Set the value of *a* at index *b* to *c*. |
| 234 | |
| 235 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | Many operations have an "in-place" version. The following functions provide a |
| 237 | more primitive access to in-place operators than the usual syntax does; for |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 238 | example, the :term:`statement` ``x += y`` is equivalent to |
| 239 | ``x = operator.iadd(x, y)``. Another way to put it is to say that |
| 240 | ``z = operator.iadd(x, y)`` is equivalent to the compound statement |
| 241 | ``z = x; z += y``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 | |
| 243 | .. function:: iadd(a, b) |
| 244 | __iadd__(a, b) |
| 245 | |
| 246 | ``a = iadd(a, b)`` is equivalent to ``a += b``. |
| 247 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 248 | |
| 249 | .. function:: iand(a, b) |
| 250 | __iand__(a, b) |
| 251 | |
| 252 | ``a = iand(a, b)`` is equivalent to ``a &= b``. |
| 253 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | |
| 255 | .. function:: iconcat(a, b) |
| 256 | __iconcat__(a, b) |
| 257 | |
| 258 | ``a = iconcat(a, b)`` is equivalent to ``a += b`` for *a* and *b* sequences. |
| 259 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 261 | .. function:: ifloordiv(a, b) |
| 262 | __ifloordiv__(a, b) |
| 263 | |
| 264 | ``a = ifloordiv(a, b)`` is equivalent to ``a //= b``. |
| 265 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 266 | |
| 267 | .. function:: ilshift(a, b) |
| 268 | __ilshift__(a, b) |
| 269 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 270 | ``a = ilshift(a, b)`` is equivalent to ``a <<= b``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | .. function:: imod(a, b) |
| 274 | __imod__(a, b) |
| 275 | |
| 276 | ``a = imod(a, b)`` is equivalent to ``a %= b``. |
| 277 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 278 | |
| 279 | .. function:: imul(a, b) |
| 280 | __imul__(a, b) |
| 281 | |
| 282 | ``a = imul(a, b)`` is equivalent to ``a *= b``. |
| 283 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
| 285 | .. function:: ior(a, b) |
| 286 | __ior__(a, b) |
| 287 | |
| 288 | ``a = ior(a, b)`` is equivalent to ``a |= b``. |
| 289 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 290 | |
| 291 | .. function:: ipow(a, b) |
| 292 | __ipow__(a, b) |
| 293 | |
| 294 | ``a = ipow(a, b)`` is equivalent to ``a **= b``. |
| 295 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 296 | |
| 297 | .. function:: irepeat(a, b) |
| 298 | __irepeat__(a, b) |
| 299 | |
| 300 | ``a = irepeat(a, b)`` is equivalent to ``a *= b`` where *a* is a sequence and |
| 301 | *b* is an integer. |
| 302 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 303 | |
| 304 | .. function:: irshift(a, b) |
| 305 | __irshift__(a, b) |
| 306 | |
| 307 | ``a = irshift(a, b)`` is equivalent to ``a >>= b``. |
| 308 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 309 | |
| 310 | .. function:: isub(a, b) |
| 311 | __isub__(a, b) |
| 312 | |
| 313 | ``a = isub(a, b)`` is equivalent to ``a -= b``. |
| 314 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 315 | |
| 316 | .. function:: itruediv(a, b) |
| 317 | __itruediv__(a, b) |
| 318 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 319 | ``a = itruediv(a, b)`` is equivalent to ``a /= b``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 320 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 321 | |
| 322 | .. function:: ixor(a, b) |
| 323 | __ixor__(a, b) |
| 324 | |
| 325 | ``a = ixor(a, b)`` is equivalent to ``a ^= b``. |
| 326 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 327 | |
| 328 | The :mod:`operator` module also defines a few predicates to test the type of |
| 329 | objects. |
| 330 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 331 | .. XXX just remove them? |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 332 | .. note:: |
| 333 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 334 | Be careful not to misinterpret the results of these functions; none have any |
| 335 | measure of reliability with instance objects. |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 336 | For example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 | |
| 338 | >>> class C: |
| 339 | ... pass |
| 340 | ... |
| 341 | >>> import operator |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 342 | >>> obj = C() |
| 343 | >>> operator.isMappingType(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 344 | True |
| 345 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 346 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 347 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 348 | Since there are now abstract classes for collection types, you should write, |
| 349 | for example, ``isinstance(obj, collections.Mapping)`` and ``isinstance(obj, |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 350 | collections.Sequence)``. |
| 351 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 352 | .. function:: isMappingType(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 353 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 354 | Returns true if the object *obj* supports the mapping interface. This is true for |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 355 | dictionaries and all instance objects defining :meth:`__getitem__`. |
| 356 | |
| 357 | .. warning:: |
| 358 | |
| 359 | There is no reliable way to test if an instance supports the complete mapping |
| 360 | protocol since the interface itself is ill-defined. This makes this test less |
| 361 | useful than it otherwise might be. |
| 362 | |
| 363 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 364 | .. function:: isNumberType(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 365 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 366 | Returns true if the object *obj* represents a number. This is true for all |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 367 | numeric types implemented in C. |
| 368 | |
| 369 | .. warning:: |
| 370 | |
| 371 | There is no reliable way to test if an instance supports the complete numeric |
| 372 | interface since the interface itself is ill-defined. This makes this test less |
| 373 | useful than it otherwise might be. |
| 374 | |
| 375 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 376 | .. function:: isSequenceType(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 377 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 378 | Returns true if the object *obj* supports the sequence protocol. This returns true |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 379 | for all objects which define sequence methods in C, and for all instance objects |
| 380 | defining :meth:`__getitem__`. |
| 381 | |
| 382 | .. warning:: |
| 383 | |
| 384 | There is no reliable way to test if an instance supports the complete sequence |
| 385 | interface since the interface itself is ill-defined. This makes this test less |
| 386 | useful than it otherwise might be. |
| 387 | |
| 388 | Example: Build a dictionary that maps the ordinals from ``0`` to ``255`` to |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 389 | their character equivalents. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 390 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 391 | >>> d = {} |
| 392 | >>> keys = range(256) |
| 393 | >>> vals = map(chr, keys) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 394 | >>> map(operator.setitem, [d]*len(keys), keys, vals) # doctest: +SKIP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 395 | |
| 396 | .. XXX: find a better, readable, example |
| 397 | |
| 398 | The :mod:`operator` module also defines tools for generalized attribute and item |
| 399 | lookups. These are useful for making fast field extractors as arguments for |
| 400 | :func:`map`, :func:`sorted`, :meth:`itertools.groupby`, or other functions that |
| 401 | expect a function argument. |
| 402 | |
| 403 | |
| 404 | .. function:: attrgetter(attr[, args...]) |
| 405 | |
| 406 | Return a callable object that fetches *attr* from its operand. If more than one |
| 407 | attribute is requested, returns a tuple of attributes. After, |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 408 | ``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After, |
| 409 | ``f = attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 410 | b.date)``. |
| 411 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 412 | The attribute names can also contain dots; after ``f = attrgetter('date.month')``, |
| 413 | the call ``f(b)`` returns ``b.date.month``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 414 | |
| 415 | .. function:: itemgetter(item[, args...]) |
| 416 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 417 | Return a callable object that fetches *item* from its operand using the |
| 418 | operand's :meth:`__getitem__` method. If multiple items are specified, |
| 419 | returns a tuple of lookup values. Equivalent to:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 420 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 421 | def itemgetter(*items): |
| 422 | if len(items) == 1: |
| 423 | item = items[0] |
| 424 | def g(obj): |
| 425 | return obj[item] |
| 426 | else: |
| 427 | def g(obj): |
| 428 | return tuple(obj[item] for item in items) |
| 429 | return g |
| 430 | |
| 431 | The items can be any type accepted by the operand's :meth:`__getitem__` |
| 432 | method. Dictionaries accept any hashable value. Lists, tuples, and |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 433 | strings accept an index or a slice: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 434 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 435 | >>> itemgetter(1)('ABCDEFG') |
| 436 | 'B' |
| 437 | >>> itemgetter(1,3,5)('ABCDEFG') |
| 438 | ('B', 'D', 'F') |
| 439 | >>> itemgetter(slice(2,None))('ABCDEFG') |
| 440 | 'CDEFG' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 441 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 442 | |
| 443 | Example of using :func:`itemgetter` to retrieve specific fields from a |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 444 | tuple record: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 445 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 446 | >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] |
| 447 | >>> getcount = itemgetter(1) |
| 448 | >>> map(getcount, inventory) |
| 449 | [3, 2, 5, 1] |
| 450 | >>> sorted(inventory, key=getcount) |
| 451 | [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 452 | |
| 453 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 454 | .. function:: methodcaller(name[, args...]) |
| 455 | |
| 456 | Return a callable object that calls the method *name* on its operand. If |
| 457 | additional arguments and/or keyword arguments are given, they will be given |
| 458 | to the method as well. After ``f = methodcaller('name')``, the call ``f(b)`` |
| 459 | returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the |
| 460 | call ``f(b)`` returns ``b.name('foo', bar=1)``. |
| 461 | |
| 462 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 463 | .. _operator-map: |
| 464 | |
| 465 | Mapping Operators to Functions |
| 466 | ------------------------------ |
| 467 | |
| 468 | This table shows how abstract operations correspond to operator symbols in the |
| 469 | Python syntax and the functions in the :mod:`operator` module. |
| 470 | |
| 471 | +-----------------------+-------------------------+---------------------------------+ |
| 472 | | Operation | Syntax | Function | |
| 473 | +=======================+=========================+=================================+ |
| 474 | | Addition | ``a + b`` | ``add(a, b)`` | |
| 475 | +-----------------------+-------------------------+---------------------------------+ |
| 476 | | Concatenation | ``seq1 + seq2`` | ``concat(seq1, seq2)`` | |
| 477 | +-----------------------+-------------------------+---------------------------------+ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 478 | | Containment Test | ``obj in seq`` | ``contains(seq, obj)`` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 479 | +-----------------------+-------------------------+---------------------------------+ |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 480 | | Division | ``a / b`` | ``truediv(a, b)`` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 481 | +-----------------------+-------------------------+---------------------------------+ |
| 482 | | Division | ``a // b`` | ``floordiv(a, b)`` | |
| 483 | +-----------------------+-------------------------+---------------------------------+ |
| 484 | | Bitwise And | ``a & b`` | ``and_(a, b)`` | |
| 485 | +-----------------------+-------------------------+---------------------------------+ |
| 486 | | Bitwise Exclusive Or | ``a ^ b`` | ``xor(a, b)`` | |
| 487 | +-----------------------+-------------------------+---------------------------------+ |
| 488 | | Bitwise Inversion | ``~ a`` | ``invert(a)`` | |
| 489 | +-----------------------+-------------------------+---------------------------------+ |
| 490 | | Bitwise Or | ``a | b`` | ``or_(a, b)`` | |
| 491 | +-----------------------+-------------------------+---------------------------------+ |
| 492 | | Exponentiation | ``a ** b`` | ``pow(a, b)`` | |
| 493 | +-----------------------+-------------------------+---------------------------------+ |
| 494 | | Identity | ``a is b`` | ``is_(a, b)`` | |
| 495 | +-----------------------+-------------------------+---------------------------------+ |
| 496 | | Identity | ``a is not b`` | ``is_not(a, b)`` | |
| 497 | +-----------------------+-------------------------+---------------------------------+ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 498 | | Indexed Assignment | ``obj[k] = v`` | ``setitem(obj, k, v)`` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 499 | +-----------------------+-------------------------+---------------------------------+ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 500 | | Indexed Deletion | ``del obj[k]`` | ``delitem(obj, k)`` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 501 | +-----------------------+-------------------------+---------------------------------+ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 502 | | Indexing | ``obj[k]`` | ``getitem(obj, k)`` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 503 | +-----------------------+-------------------------+---------------------------------+ |
| 504 | | Left Shift | ``a << b`` | ``lshift(a, b)`` | |
| 505 | +-----------------------+-------------------------+---------------------------------+ |
| 506 | | Modulo | ``a % b`` | ``mod(a, b)`` | |
| 507 | +-----------------------+-------------------------+---------------------------------+ |
| 508 | | Multiplication | ``a * b`` | ``mul(a, b)`` | |
| 509 | +-----------------------+-------------------------+---------------------------------+ |
| 510 | | Negation (Arithmetic) | ``- a`` | ``neg(a)`` | |
| 511 | +-----------------------+-------------------------+---------------------------------+ |
| 512 | | Negation (Logical) | ``not a`` | ``not_(a)`` | |
| 513 | +-----------------------+-------------------------+---------------------------------+ |
| 514 | | Right Shift | ``a >> b`` | ``rshift(a, b)`` | |
| 515 | +-----------------------+-------------------------+---------------------------------+ |
| 516 | | Sequence Repitition | ``seq * i`` | ``repeat(seq, i)`` | |
| 517 | +-----------------------+-------------------------+---------------------------------+ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 518 | | String Formatting | ``s % obj`` | ``mod(s, obj)`` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 519 | +-----------------------+-------------------------+---------------------------------+ |
| 520 | | Subtraction | ``a - b`` | ``sub(a, b)`` | |
| 521 | +-----------------------+-------------------------+---------------------------------+ |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 522 | | Truth Test | ``obj`` | ``truth(obj)`` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 523 | +-----------------------+-------------------------+---------------------------------+ |
| 524 | | Ordering | ``a < b`` | ``lt(a, b)`` | |
| 525 | +-----------------------+-------------------------+---------------------------------+ |
| 526 | | Ordering | ``a <= b`` | ``le(a, b)`` | |
| 527 | +-----------------------+-------------------------+---------------------------------+ |
| 528 | | Equality | ``a == b`` | ``eq(a, b)`` | |
| 529 | +-----------------------+-------------------------+---------------------------------+ |
| 530 | | Difference | ``a != b`` | ``ne(a, b)`` | |
| 531 | +-----------------------+-------------------------+---------------------------------+ |
| 532 | | Ordering | ``a >= b`` | ``ge(a, b)`` | |
| 533 | +-----------------------+-------------------------+---------------------------------+ |
| 534 | | Ordering | ``a > b`` | ``gt(a, b)`` | |
| 535 | +-----------------------+-------------------------+---------------------------------+ |
| 536 | |