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:: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 10 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 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 |
Mark Dickinson | c48d834 | 2009-02-01 14:18:10 +0000 | [diff] [blame] | 46 | >= b``. Note that these functions can return any value, which may |
| 47 | or may not be interpretable as a Boolean value. See |
| 48 | :ref:`comparisons` for more information about rich comparisons. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 107 | .. function:: index(a) |
| 108 | __index__(a) |
| 109 | |
| 110 | Return *a* converted to an integer. Equivalent to ``a.__index__()``. |
| 111 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 112 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 113 | .. function:: inv(obj) |
| 114 | invert(obj) |
| 115 | __inv__(obj) |
| 116 | __invert__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 118 | 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] | 119 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 120 | |
| 121 | .. function:: lshift(a, b) |
| 122 | __lshift__(a, b) |
| 123 | |
| 124 | Return *a* shifted left by *b*. |
| 125 | |
| 126 | |
| 127 | .. function:: mod(a, b) |
| 128 | __mod__(a, b) |
| 129 | |
| 130 | Return ``a % b``. |
| 131 | |
| 132 | |
| 133 | .. function:: mul(a, b) |
| 134 | __mul__(a, b) |
| 135 | |
| 136 | Return ``a * b``, for *a* and *b* numbers. |
| 137 | |
| 138 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 139 | .. function:: neg(obj) |
| 140 | __neg__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 142 | Return *obj* negated (``-obj``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
| 144 | |
| 145 | .. function:: or_(a, b) |
| 146 | __or__(a, b) |
| 147 | |
| 148 | Return the bitwise or of *a* and *b*. |
| 149 | |
| 150 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 151 | .. function:: pos(obj) |
| 152 | __pos__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 154 | Return *obj* positive (``+obj``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | .. function:: pow(a, b) |
| 158 | __pow__(a, b) |
| 159 | |
| 160 | Return ``a ** b``, for *a* and *b* numbers. |
| 161 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
| 163 | .. function:: rshift(a, b) |
| 164 | __rshift__(a, b) |
| 165 | |
| 166 | Return *a* shifted right by *b*. |
| 167 | |
| 168 | |
| 169 | .. function:: sub(a, b) |
| 170 | __sub__(a, b) |
| 171 | |
| 172 | Return ``a - b``. |
| 173 | |
| 174 | |
| 175 | .. function:: truediv(a, b) |
| 176 | __truediv__(a, b) |
| 177 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 178 | Return ``a / b`` where 2/3 is .66 rather than 0. This is also known as |
| 179 | "true" division. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 181 | |
| 182 | .. function:: xor(a, b) |
| 183 | __xor__(a, b) |
| 184 | |
| 185 | Return the bitwise exclusive or of *a* and *b*. |
| 186 | |
| 187 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 188 | Operations which work with sequences (some of them with mappings too) include: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | 48310cd | 2009-01-03 21:18:54 +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 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | .. function:: setitem(a, b, c) |
| 225 | __setitem__(a, b, c) |
| 226 | |
| 227 | Set the value of *a* at index *b* to *c*. |
| 228 | |
| 229 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 230 | Many operations have an "in-place" version. The following functions provide a |
| 231 | 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] | 232 | example, the :term:`statement` ``x += y`` is equivalent to |
| 233 | ``x = operator.iadd(x, y)``. Another way to put it is to say that |
| 234 | ``z = operator.iadd(x, y)`` is equivalent to the compound statement |
| 235 | ``z = x; z += y``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
| 237 | .. function:: iadd(a, b) |
| 238 | __iadd__(a, b) |
| 239 | |
| 240 | ``a = iadd(a, b)`` is equivalent to ``a += b``. |
| 241 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 | |
| 243 | .. function:: iand(a, b) |
| 244 | __iand__(a, b) |
| 245 | |
| 246 | ``a = iand(a, b)`` is equivalent to ``a &= b``. |
| 247 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 248 | |
| 249 | .. function:: iconcat(a, b) |
| 250 | __iconcat__(a, b) |
| 251 | |
| 252 | ``a = iconcat(a, b)`` is equivalent to ``a += b`` for *a* and *b* sequences. |
| 253 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 255 | .. function:: ifloordiv(a, b) |
| 256 | __ifloordiv__(a, b) |
| 257 | |
| 258 | ``a = ifloordiv(a, b)`` is equivalent to ``a //= b``. |
| 259 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | |
| 261 | .. function:: ilshift(a, b) |
| 262 | __ilshift__(a, b) |
| 263 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 264 | ``a = ilshift(a, b)`` is equivalent to ``a <<= b``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 265 | |
| 266 | |
| 267 | .. function:: imod(a, b) |
| 268 | __imod__(a, b) |
| 269 | |
| 270 | ``a = imod(a, b)`` is equivalent to ``a %= b``. |
| 271 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | |
| 273 | .. function:: imul(a, b) |
| 274 | __imul__(a, b) |
| 275 | |
| 276 | ``a = imul(a, b)`` is equivalent to ``a *= b``. |
| 277 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 278 | |
| 279 | .. function:: ior(a, b) |
| 280 | __ior__(a, b) |
| 281 | |
| 282 | ``a = ior(a, b)`` is equivalent to ``a |= b``. |
| 283 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
| 285 | .. function:: ipow(a, b) |
| 286 | __ipow__(a, b) |
| 287 | |
| 288 | ``a = ipow(a, b)`` is equivalent to ``a **= b``. |
| 289 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 290 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 291 | .. function:: irshift(a, b) |
| 292 | __irshift__(a, b) |
| 293 | |
| 294 | ``a = irshift(a, b)`` is equivalent to ``a >>= b``. |
| 295 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 296 | |
| 297 | .. function:: isub(a, b) |
| 298 | __isub__(a, b) |
| 299 | |
| 300 | ``a = isub(a, b)`` is equivalent to ``a -= b``. |
| 301 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 302 | |
| 303 | .. function:: itruediv(a, b) |
| 304 | __itruediv__(a, b) |
| 305 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 306 | ``a = itruediv(a, b)`` is equivalent to ``a /= b``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 307 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 308 | |
| 309 | .. function:: ixor(a, b) |
| 310 | __ixor__(a, b) |
| 311 | |
| 312 | ``a = ixor(a, b)`` is equivalent to ``a ^= b``. |
| 313 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 314 | 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] | 315 | their character equivalents. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 316 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 | >>> d = {} |
| 318 | >>> keys = range(256) |
| 319 | >>> vals = map(chr, keys) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 320 | >>> map(operator.setitem, [d]*len(keys), keys, vals) # doctest: +SKIP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 321 | |
| 322 | .. XXX: find a better, readable, example |
| 323 | |
| 324 | The :mod:`operator` module also defines tools for generalized attribute and item |
| 325 | lookups. These are useful for making fast field extractors as arguments for |
| 326 | :func:`map`, :func:`sorted`, :meth:`itertools.groupby`, or other functions that |
| 327 | expect a function argument. |
| 328 | |
| 329 | |
| 330 | .. function:: attrgetter(attr[, args...]) |
| 331 | |
| 332 | Return a callable object that fetches *attr* from its operand. If more than one |
| 333 | attribute is requested, returns a tuple of attributes. After, |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 334 | ``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After, |
| 335 | ``f = attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name, |
Benjamin Peterson | 2d55e2a | 2010-08-21 20:08:36 +0000 | [diff] [blame] | 336 | b.date)``. Equivalent to:: |
| 337 | |
| 338 | def attrgetter(*items): |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 339 | if any(not isinstance(item, str) for item in items): |
| 340 | raise TypeError('attribute name must be a string') |
Benjamin Peterson | 2d55e2a | 2010-08-21 20:08:36 +0000 | [diff] [blame] | 341 | if len(items) == 1: |
| 342 | attr = items[0] |
| 343 | def g(obj): |
| 344 | return resolve_attr(obj, attr) |
| 345 | else: |
| 346 | def g(obj): |
| 347 | return tuple(resolve_att(obj, attr) for attr in items) |
| 348 | return g |
| 349 | |
| 350 | def resolve_attr(obj, attr): |
| 351 | for name in attr.split("."): |
| 352 | obj = getattr(obj, name) |
| 353 | return obj |
| 354 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 355 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 356 | The attribute names can also contain dots; after ``f = attrgetter('date.month')``, |
| 357 | the call ``f(b)`` returns ``b.date.month``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 358 | |
| 359 | .. function:: itemgetter(item[, args...]) |
| 360 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 361 | Return a callable object that fetches *item* from its operand using the |
| 362 | operand's :meth:`__getitem__` method. If multiple items are specified, |
| 363 | returns a tuple of lookup values. Equivalent to:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 364 | |
Benjamin Peterson | ffec810 | 2010-08-21 20:01:28 +0000 | [diff] [blame] | 365 | def itemgetter(*items): |
| 366 | if len(items) == 1: |
| 367 | item = items[0] |
| 368 | def g(obj): |
| 369 | return obj[item] |
| 370 | else: |
| 371 | def g(obj): |
| 372 | return tuple(obj[item] for item in items) |
| 373 | return g |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 374 | |
| 375 | The items can be any type accepted by the operand's :meth:`__getitem__` |
| 376 | method. Dictionaries accept any hashable value. Lists, tuples, and |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 377 | strings accept an index or a slice: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 378 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 379 | >>> itemgetter(1)('ABCDEFG') |
| 380 | 'B' |
| 381 | >>> itemgetter(1,3,5)('ABCDEFG') |
| 382 | ('B', 'D', 'F') |
| 383 | >>> itemgetter(slice(2,None))('ABCDEFG') |
| 384 | 'CDEFG' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 385 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 386 | |
| 387 | Example of using :func:`itemgetter` to retrieve specific fields from a |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 388 | tuple record: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 389 | |
Benjamin Peterson | c16f8b3 | 2010-08-21 20:03:15 +0000 | [diff] [blame] | 390 | >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] |
| 391 | >>> getcount = itemgetter(1) |
Raymond Hettinger | d292a17 | 2010-09-01 07:46:54 +0000 | [diff] [blame] | 392 | >>> list(map(getcount, inventory)) |
Benjamin Peterson | c16f8b3 | 2010-08-21 20:03:15 +0000 | [diff] [blame] | 393 | [3, 2, 5, 1] |
| 394 | >>> sorted(inventory, key=getcount) |
| 395 | [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 396 | |
| 397 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 398 | .. function:: methodcaller(name[, args...]) |
| 399 | |
| 400 | Return a callable object that calls the method *name* on its operand. If |
| 401 | additional arguments and/or keyword arguments are given, they will be given |
| 402 | to the method as well. After ``f = methodcaller('name')``, the call ``f(b)`` |
| 403 | returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the |
Benjamin Peterson | 2d55e2a | 2010-08-21 20:08:36 +0000 | [diff] [blame] | 404 | call ``f(b)`` returns ``b.name('foo', bar=1)``. Equivalent to:: |
| 405 | |
| 406 | def methodcaller(name, *args, **kwargs): |
| 407 | def caller(obj): |
| 408 | return getattr(obj, name)(*args, **kwargs) |
| 409 | return caller |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 410 | |
| 411 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 412 | .. _operator-map: |
| 413 | |
| 414 | Mapping Operators to Functions |
| 415 | ------------------------------ |
| 416 | |
| 417 | This table shows how abstract operations correspond to operator symbols in the |
| 418 | Python syntax and the functions in the :mod:`operator` module. |
| 419 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 420 | +-----------------------+-------------------------+---------------------------------------+ |
| 421 | | Operation | Syntax | Function | |
| 422 | +=======================+=========================+=======================================+ |
| 423 | | Addition | ``a + b`` | ``add(a, b)`` | |
| 424 | +-----------------------+-------------------------+---------------------------------------+ |
| 425 | | Concatenation | ``seq1 + seq2`` | ``concat(seq1, seq2)`` | |
| 426 | +-----------------------+-------------------------+---------------------------------------+ |
| 427 | | Containment Test | ``obj in seq`` | ``contains(seq, obj)`` | |
| 428 | +-----------------------+-------------------------+---------------------------------------+ |
| 429 | | Division | ``a / b`` | ``div(a, b)`` | |
| 430 | +-----------------------+-------------------------+---------------------------------------+ |
| 431 | | Division | ``a // b`` | ``floordiv(a, b)`` | |
| 432 | +-----------------------+-------------------------+---------------------------------------+ |
| 433 | | Bitwise And | ``a & b`` | ``and_(a, b)`` | |
| 434 | +-----------------------+-------------------------+---------------------------------------+ |
| 435 | | Bitwise Exclusive Or | ``a ^ b`` | ``xor(a, b)`` | |
| 436 | +-----------------------+-------------------------+---------------------------------------+ |
| 437 | | Bitwise Inversion | ``~ a`` | ``invert(a)`` | |
| 438 | +-----------------------+-------------------------+---------------------------------------+ |
| 439 | | Bitwise Or | ``a | b`` | ``or_(a, b)`` | |
| 440 | +-----------------------+-------------------------+---------------------------------------+ |
| 441 | | Exponentiation | ``a ** b`` | ``pow(a, b)`` | |
| 442 | +-----------------------+-------------------------+---------------------------------------+ |
| 443 | | Identity | ``a is b`` | ``is_(a, b)`` | |
| 444 | +-----------------------+-------------------------+---------------------------------------+ |
| 445 | | Identity | ``a is not b`` | ``is_not(a, b)`` | |
| 446 | +-----------------------+-------------------------+---------------------------------------+ |
| 447 | | Indexed Assignment | ``obj[k] = v`` | ``setitem(obj, k, v)`` | |
| 448 | +-----------------------+-------------------------+---------------------------------------+ |
| 449 | | Indexed Deletion | ``del obj[k]`` | ``delitem(obj, k)`` | |
| 450 | +-----------------------+-------------------------+---------------------------------------+ |
| 451 | | Indexing | ``obj[k]`` | ``getitem(obj, k)`` | |
| 452 | +-----------------------+-------------------------+---------------------------------------+ |
| 453 | | Left Shift | ``a << b`` | ``lshift(a, b)`` | |
| 454 | +-----------------------+-------------------------+---------------------------------------+ |
| 455 | | Modulo | ``a % b`` | ``mod(a, b)`` | |
| 456 | +-----------------------+-------------------------+---------------------------------------+ |
| 457 | | Multiplication | ``a * b`` | ``mul(a, b)`` | |
| 458 | +-----------------------+-------------------------+---------------------------------------+ |
| 459 | | Negation (Arithmetic) | ``- a`` | ``neg(a)`` | |
| 460 | +-----------------------+-------------------------+---------------------------------------+ |
| 461 | | Negation (Logical) | ``not a`` | ``not_(a)`` | |
| 462 | +-----------------------+-------------------------+---------------------------------------+ |
| 463 | | Positive | ``+ a`` | ``pos(a)`` | |
| 464 | +-----------------------+-------------------------+---------------------------------------+ |
| 465 | | Right Shift | ``a >> b`` | ``rshift(a, b)`` | |
| 466 | +-----------------------+-------------------------+---------------------------------------+ |
| 467 | | Sequence Repetition | ``seq * i`` | ``repeat(seq, i)`` | |
| 468 | +-----------------------+-------------------------+---------------------------------------+ |
| 469 | | Slice Assignment | ``seq[i:j] = values`` | ``setitem(seq, slice(i, j), values)`` | |
| 470 | +-----------------------+-------------------------+---------------------------------------+ |
| 471 | | Slice Deletion | ``del seq[i:j]`` | ``delitem(seq, slice(i, j))`` | |
| 472 | +-----------------------+-------------------------+---------------------------------------+ |
| 473 | | Slicing | ``seq[i:j]`` | ``getitem(seq, slice(i, j))`` | |
| 474 | +-----------------------+-------------------------+---------------------------------------+ |
| 475 | | String Formatting | ``s % obj`` | ``mod(s, obj)`` | |
| 476 | +-----------------------+-------------------------+---------------------------------------+ |
| 477 | | Subtraction | ``a - b`` | ``sub(a, b)`` | |
| 478 | +-----------------------+-------------------------+---------------------------------------+ |
| 479 | | Truth Test | ``obj`` | ``truth(obj)`` | |
| 480 | +-----------------------+-------------------------+---------------------------------------+ |
| 481 | | Ordering | ``a < b`` | ``lt(a, b)`` | |
| 482 | +-----------------------+-------------------------+---------------------------------------+ |
| 483 | | Ordering | ``a <= b`` | ``le(a, b)`` | |
| 484 | +-----------------------+-------------------------+---------------------------------------+ |
| 485 | | Equality | ``a == b`` | ``eq(a, b)`` | |
| 486 | +-----------------------+-------------------------+---------------------------------------+ |
| 487 | | Difference | ``a != b`` | ``ne(a, b)`` | |
| 488 | +-----------------------+-------------------------+---------------------------------------+ |
| 489 | | Ordering | ``a >= b`` | ``ge(a, b)`` | |
| 490 | +-----------------------+-------------------------+---------------------------------------+ |
| 491 | | Ordering | ``a > b`` | ``gt(a, b)`` | |
| 492 | +-----------------------+-------------------------+---------------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 493 | |