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, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 336 | b.date)``. |
| 337 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 338 | The attribute names can also contain dots; after ``f = attrgetter('date.month')``, |
| 339 | the call ``f(b)`` returns ``b.date.month``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 340 | |
| 341 | .. function:: itemgetter(item[, args...]) |
| 342 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 343 | Return a callable object that fetches *item* from its operand using the |
| 344 | operand's :meth:`__getitem__` method. If multiple items are specified, |
| 345 | returns a tuple of lookup values. Equivalent to:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 347 | def itemgetter(*items): |
| 348 | if len(items) == 1: |
| 349 | item = items[0] |
| 350 | def g(obj): |
| 351 | return obj[item] |
| 352 | else: |
| 353 | def g(obj): |
| 354 | return tuple(obj[item] for item in items) |
| 355 | return g |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 356 | |
| 357 | The items can be any type accepted by the operand's :meth:`__getitem__` |
| 358 | method. Dictionaries accept any hashable value. Lists, tuples, and |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 359 | strings accept an index or a slice: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 360 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 361 | >>> itemgetter(1)('ABCDEFG') |
| 362 | 'B' |
| 363 | >>> itemgetter(1,3,5)('ABCDEFG') |
| 364 | ('B', 'D', 'F') |
| 365 | >>> itemgetter(slice(2,None))('ABCDEFG') |
| 366 | 'CDEFG' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 367 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 368 | |
| 369 | Example of using :func:`itemgetter` to retrieve specific fields from a |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 370 | tuple record: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 371 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 372 | >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] |
| 373 | >>> getcount = itemgetter(1) |
| 374 | >>> map(getcount, inventory) |
| 375 | [3, 2, 5, 1] |
| 376 | >>> sorted(inventory, key=getcount) |
| 377 | [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 378 | |
| 379 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 380 | .. function:: methodcaller(name[, args...]) |
| 381 | |
| 382 | Return a callable object that calls the method *name* on its operand. If |
| 383 | additional arguments and/or keyword arguments are given, they will be given |
| 384 | to the method as well. After ``f = methodcaller('name')``, the call ``f(b)`` |
| 385 | returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the |
| 386 | call ``f(b)`` returns ``b.name('foo', bar=1)``. |
| 387 | |
| 388 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 389 | .. _operator-map: |
| 390 | |
| 391 | Mapping Operators to Functions |
| 392 | ------------------------------ |
| 393 | |
| 394 | This table shows how abstract operations correspond to operator symbols in the |
| 395 | Python syntax and the functions in the :mod:`operator` module. |
| 396 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 397 | +-----------------------+-------------------------+---------------------------------------+ |
| 398 | | Operation | Syntax | Function | |
| 399 | +=======================+=========================+=======================================+ |
| 400 | | Addition | ``a + b`` | ``add(a, b)`` | |
| 401 | +-----------------------+-------------------------+---------------------------------------+ |
| 402 | | Concatenation | ``seq1 + seq2`` | ``concat(seq1, seq2)`` | |
| 403 | +-----------------------+-------------------------+---------------------------------------+ |
| 404 | | Containment Test | ``obj in seq`` | ``contains(seq, obj)`` | |
| 405 | +-----------------------+-------------------------+---------------------------------------+ |
| 406 | | Division | ``a / b`` | ``div(a, b)`` | |
| 407 | +-----------------------+-------------------------+---------------------------------------+ |
| 408 | | Division | ``a // b`` | ``floordiv(a, b)`` | |
| 409 | +-----------------------+-------------------------+---------------------------------------+ |
| 410 | | Bitwise And | ``a & b`` | ``and_(a, b)`` | |
| 411 | +-----------------------+-------------------------+---------------------------------------+ |
| 412 | | Bitwise Exclusive Or | ``a ^ b`` | ``xor(a, b)`` | |
| 413 | +-----------------------+-------------------------+---------------------------------------+ |
| 414 | | Bitwise Inversion | ``~ a`` | ``invert(a)`` | |
| 415 | +-----------------------+-------------------------+---------------------------------------+ |
| 416 | | Bitwise Or | ``a | b`` | ``or_(a, b)`` | |
| 417 | +-----------------------+-------------------------+---------------------------------------+ |
| 418 | | Exponentiation | ``a ** b`` | ``pow(a, b)`` | |
| 419 | +-----------------------+-------------------------+---------------------------------------+ |
| 420 | | Identity | ``a is b`` | ``is_(a, b)`` | |
| 421 | +-----------------------+-------------------------+---------------------------------------+ |
| 422 | | Identity | ``a is not b`` | ``is_not(a, b)`` | |
| 423 | +-----------------------+-------------------------+---------------------------------------+ |
| 424 | | Indexed Assignment | ``obj[k] = v`` | ``setitem(obj, k, v)`` | |
| 425 | +-----------------------+-------------------------+---------------------------------------+ |
| 426 | | Indexed Deletion | ``del obj[k]`` | ``delitem(obj, k)`` | |
| 427 | +-----------------------+-------------------------+---------------------------------------+ |
| 428 | | Indexing | ``obj[k]`` | ``getitem(obj, k)`` | |
| 429 | +-----------------------+-------------------------+---------------------------------------+ |
| 430 | | Left Shift | ``a << b`` | ``lshift(a, b)`` | |
| 431 | +-----------------------+-------------------------+---------------------------------------+ |
| 432 | | Modulo | ``a % b`` | ``mod(a, b)`` | |
| 433 | +-----------------------+-------------------------+---------------------------------------+ |
| 434 | | Multiplication | ``a * b`` | ``mul(a, b)`` | |
| 435 | +-----------------------+-------------------------+---------------------------------------+ |
| 436 | | Negation (Arithmetic) | ``- a`` | ``neg(a)`` | |
| 437 | +-----------------------+-------------------------+---------------------------------------+ |
| 438 | | Negation (Logical) | ``not a`` | ``not_(a)`` | |
| 439 | +-----------------------+-------------------------+---------------------------------------+ |
| 440 | | Positive | ``+ a`` | ``pos(a)`` | |
| 441 | +-----------------------+-------------------------+---------------------------------------+ |
| 442 | | Right Shift | ``a >> b`` | ``rshift(a, b)`` | |
| 443 | +-----------------------+-------------------------+---------------------------------------+ |
| 444 | | Sequence Repetition | ``seq * i`` | ``repeat(seq, i)`` | |
| 445 | +-----------------------+-------------------------+---------------------------------------+ |
| 446 | | Slice Assignment | ``seq[i:j] = values`` | ``setitem(seq, slice(i, j), values)`` | |
| 447 | +-----------------------+-------------------------+---------------------------------------+ |
| 448 | | Slice Deletion | ``del seq[i:j]`` | ``delitem(seq, slice(i, j))`` | |
| 449 | +-----------------------+-------------------------+---------------------------------------+ |
| 450 | | Slicing | ``seq[i:j]`` | ``getitem(seq, slice(i, j))`` | |
| 451 | +-----------------------+-------------------------+---------------------------------------+ |
| 452 | | String Formatting | ``s % obj`` | ``mod(s, obj)`` | |
| 453 | +-----------------------+-------------------------+---------------------------------------+ |
| 454 | | Subtraction | ``a - b`` | ``sub(a, b)`` | |
| 455 | +-----------------------+-------------------------+---------------------------------------+ |
| 456 | | Truth Test | ``obj`` | ``truth(obj)`` | |
| 457 | +-----------------------+-------------------------+---------------------------------------+ |
| 458 | | Ordering | ``a < b`` | ``lt(a, b)`` | |
| 459 | +-----------------------+-------------------------+---------------------------------------+ |
| 460 | | Ordering | ``a <= b`` | ``le(a, b)`` | |
| 461 | +-----------------------+-------------------------+---------------------------------------+ |
| 462 | | Equality | ``a == b`` | ``eq(a, b)`` | |
| 463 | +-----------------------+-------------------------+---------------------------------------+ |
| 464 | | Difference | ``a != b`` | ``ne(a, b)`` | |
| 465 | +-----------------------+-------------------------+---------------------------------------+ |
| 466 | | Ordering | ``a >= b`` | ``ge(a, b)`` | |
| 467 | +-----------------------+-------------------------+---------------------------------------+ |
| 468 | | Ordering | ``a > b`` | ``gt(a, b)`` | |
| 469 | +-----------------------+-------------------------+---------------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 470 | |