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