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 |
Alexander Belopolsky | 287d1fd | 2011-01-12 16:37:14 +0000 | [diff] [blame] | 12 | from operator import itemgetter, iadd |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 13 | |
Raymond Hettinger | 3f5228d | 2013-05-10 19:57:44 -0700 | [diff] [blame] | 14 | **Source code:** :source:`Lib/operator.py` |
| 15 | |
| 16 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
Benjamin Peterson | 0f1e3ac | 2011-12-20 10:12:41 -0600 | [diff] [blame] | 18 | The :mod:`operator` module exports a set of efficient functions corresponding to |
| 19 | the intrinsic operators of Python. For example, ``operator.add(x, y)`` is |
Benjamin Peterson | 1c92cfe | 2011-12-19 16:41:11 -0500 | [diff] [blame] | 20 | equivalent to the expression ``x+y``. The function names are those used for |
| 21 | special class methods; variants without leading and trailing ``__`` are also |
| 22 | provided for convenience. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
| 24 | The functions fall into categories that perform object comparisons, logical |
Georg Brandl | b30f330 | 2011-01-06 09:23:56 +0000 | [diff] [blame] | 25 | operations, mathematical operations and sequence operations. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| 27 | The object comparison functions are useful for all objects, and are named after |
| 28 | the rich comparison operators they support: |
| 29 | |
| 30 | |
| 31 | .. function:: lt(a, b) |
| 32 | le(a, b) |
| 33 | eq(a, b) |
| 34 | ne(a, b) |
| 35 | ge(a, b) |
| 36 | gt(a, b) |
| 37 | __lt__(a, b) |
| 38 | __le__(a, b) |
| 39 | __eq__(a, b) |
| 40 | __ne__(a, b) |
| 41 | __ge__(a, b) |
| 42 | __gt__(a, b) |
| 43 | |
| 44 | Perform "rich comparisons" between *a* and *b*. Specifically, ``lt(a, b)`` is |
| 45 | equivalent to ``a < b``, ``le(a, b)`` is equivalent to ``a <= b``, ``eq(a, |
| 46 | b)`` is equivalent to ``a == b``, ``ne(a, b)`` is equivalent to ``a != b``, |
| 47 | ``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] | 48 | >= b``. Note that these functions can return any value, which may |
| 49 | or may not be interpretable as a Boolean value. See |
| 50 | :ref:`comparisons` for more information about rich comparisons. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
| 53 | The logical operations are also generally applicable to all objects, and support |
| 54 | truth tests, identity tests, and boolean operations: |
| 55 | |
| 56 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 57 | .. function:: not_(obj) |
| 58 | __not__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 60 | Return the outcome of :keyword:`not` *obj*. (Note that there is no |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | :meth:`__not__` method for object instances; only the interpreter core defines |
| 62 | this operation. The result is affected by the :meth:`__bool__` and |
| 63 | :meth:`__len__` methods.) |
| 64 | |
| 65 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 66 | .. function:: truth(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 68 | 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] | 69 | equivalent to using the :class:`bool` constructor. |
| 70 | |
| 71 | |
| 72 | .. function:: is_(a, b) |
| 73 | |
| 74 | Return ``a is b``. Tests object identity. |
| 75 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | .. function:: is_not(a, b) |
| 78 | |
| 79 | Return ``a is not b``. Tests object identity. |
| 80 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | The mathematical and bitwise operations are the most numerous: |
| 83 | |
| 84 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 85 | .. function:: abs(obj) |
| 86 | __abs__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 88 | Return the absolute value of *obj*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | .. function:: add(a, b) |
| 92 | __add__(a, b) |
| 93 | |
| 94 | Return ``a + b``, for *a* and *b* numbers. |
| 95 | |
| 96 | |
| 97 | .. function:: and_(a, b) |
| 98 | __and__(a, b) |
| 99 | |
| 100 | Return the bitwise and of *a* and *b*. |
| 101 | |
| 102 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 103 | .. function:: floordiv(a, b) |
| 104 | __floordiv__(a, b) |
| 105 | |
| 106 | Return ``a // b``. |
| 107 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 109 | .. function:: index(a) |
| 110 | __index__(a) |
| 111 | |
| 112 | Return *a* converted to an integer. Equivalent to ``a.__index__()``. |
| 113 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 114 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 115 | .. function:: inv(obj) |
| 116 | invert(obj) |
| 117 | __inv__(obj) |
| 118 | __invert__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 120 | 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] | 121 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | |
| 123 | .. function:: lshift(a, b) |
| 124 | __lshift__(a, b) |
| 125 | |
| 126 | Return *a* shifted left by *b*. |
| 127 | |
| 128 | |
| 129 | .. function:: mod(a, b) |
| 130 | __mod__(a, b) |
| 131 | |
| 132 | Return ``a % b``. |
| 133 | |
| 134 | |
| 135 | .. function:: mul(a, b) |
| 136 | __mul__(a, b) |
| 137 | |
| 138 | Return ``a * b``, for *a* and *b* numbers. |
| 139 | |
| 140 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 141 | .. function:: neg(obj) |
| 142 | __neg__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 144 | Return *obj* negated (``-obj``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | |
| 146 | |
| 147 | .. function:: or_(a, b) |
| 148 | __or__(a, b) |
| 149 | |
| 150 | Return the bitwise or of *a* and *b*. |
| 151 | |
| 152 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 153 | .. function:: pos(obj) |
| 154 | __pos__(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 156 | Return *obj* positive (``+obj``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
| 158 | |
| 159 | .. function:: pow(a, b) |
| 160 | __pow__(a, b) |
| 161 | |
| 162 | Return ``a ** b``, for *a* and *b* numbers. |
| 163 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | |
| 165 | .. function:: rshift(a, b) |
| 166 | __rshift__(a, b) |
| 167 | |
| 168 | Return *a* shifted right by *b*. |
| 169 | |
| 170 | |
| 171 | .. function:: sub(a, b) |
| 172 | __sub__(a, b) |
| 173 | |
| 174 | Return ``a - b``. |
| 175 | |
| 176 | |
| 177 | .. function:: truediv(a, b) |
| 178 | __truediv__(a, b) |
| 179 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 180 | Return ``a / b`` where 2/3 is .66 rather than 0. This is also known as |
| 181 | "true" division. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 183 | |
| 184 | .. function:: xor(a, b) |
| 185 | __xor__(a, b) |
| 186 | |
| 187 | Return the bitwise exclusive or of *a* and *b*. |
| 188 | |
| 189 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 190 | Operations which work with sequences (some of them with mappings too) include: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 191 | |
| 192 | .. function:: concat(a, b) |
| 193 | __concat__(a, b) |
| 194 | |
| 195 | Return ``a + b`` for *a* and *b* sequences. |
| 196 | |
| 197 | |
| 198 | .. function:: contains(a, b) |
| 199 | __contains__(a, b) |
| 200 | |
| 201 | Return the outcome of the test ``b in a``. Note the reversed operands. |
| 202 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | |
| 204 | .. function:: countOf(a, b) |
| 205 | |
| 206 | Return the number of occurrences of *b* in *a*. |
| 207 | |
| 208 | |
| 209 | .. function:: delitem(a, b) |
| 210 | __delitem__(a, b) |
| 211 | |
| 212 | Remove the value of *a* at index *b*. |
| 213 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 214 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 215 | .. function:: getitem(a, b) |
| 216 | __getitem__(a, b) |
| 217 | |
| 218 | Return the value of *a* at index *b*. |
| 219 | |
| 220 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 221 | .. function:: indexOf(a, b) |
| 222 | |
| 223 | Return the index of the first of occurrence of *b* in *a*. |
| 224 | |
| 225 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 226 | .. function:: setitem(a, b, c) |
| 227 | __setitem__(a, b, c) |
| 228 | |
| 229 | Set the value of *a* at index *b* to *c*. |
| 230 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | 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] | 232 | their character equivalents. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 234 | >>> d = {} |
| 235 | >>> keys = range(256) |
| 236 | >>> vals = map(chr, keys) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 237 | >>> map(operator.setitem, [d]*len(keys), keys, vals) # doctest: +SKIP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 238 | |
| 239 | .. XXX: find a better, readable, example |
| 240 | |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 241 | .. function:: length_hint(obj, default=0) |
| 242 | |
| 243 | Return an estimated length for the object *o*. First trying to return its |
Ezio Melotti | e12dc28 | 2012-10-07 12:09:36 +0300 | [diff] [blame] | 244 | actual length, then an estimate using :meth:`object.__length_hint__`, and |
| 245 | finally returning the default value. |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 246 | |
Armin Ronacher | 74b38b1 | 2012-10-07 10:29:32 +0200 | [diff] [blame] | 247 | .. versionadded:: 3.4 |
| 248 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | The :mod:`operator` module also defines tools for generalized attribute and item |
| 250 | lookups. These are useful for making fast field extractors as arguments for |
| 251 | :func:`map`, :func:`sorted`, :meth:`itertools.groupby`, or other functions that |
| 252 | expect a function argument. |
| 253 | |
| 254 | |
Ezio Melotti | babc822 | 2013-05-08 10:53:11 +0300 | [diff] [blame] | 255 | .. function:: attrgetter(attr) |
| 256 | attrgetter(*attrs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 257 | |
Ezio Melotti | babc822 | 2013-05-08 10:53:11 +0300 | [diff] [blame] | 258 | Return a callable object that fetches *attr* from its operand. |
| 259 | If more than one attribute is requested, returns a tuple of attributes. |
| 260 | The attribute names can also contain dots. For example: |
| 261 | |
| 262 | * After ``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. |
| 263 | |
| 264 | * After ``f = attrgetter('name', 'date')``, the call ``f(b)`` returns |
| 265 | ``(b.name, b.date)``. |
| 266 | |
| 267 | * After ``f = attrgetter('name.first', 'name.last')``, the call ``f(b)`` |
| 268 | returns ``(r.name.first, r.name.last)``. |
| 269 | |
| 270 | Equivalent to:: |
Benjamin Peterson | 2d55e2a | 2010-08-21 20:08:36 +0000 | [diff] [blame] | 271 | |
| 272 | def attrgetter(*items): |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 273 | if any(not isinstance(item, str) for item in items): |
| 274 | raise TypeError('attribute name must be a string') |
Benjamin Peterson | 2d55e2a | 2010-08-21 20:08:36 +0000 | [diff] [blame] | 275 | if len(items) == 1: |
| 276 | attr = items[0] |
| 277 | def g(obj): |
| 278 | return resolve_attr(obj, attr) |
| 279 | else: |
| 280 | def g(obj): |
Georg Brandl | f6d6347 | 2013-10-06 19:14:35 +0200 | [diff] [blame] | 281 | return tuple(resolve_attr(obj, attr) for attr in items) |
Benjamin Peterson | 2d55e2a | 2010-08-21 20:08:36 +0000 | [diff] [blame] | 282 | return g |
| 283 | |
| 284 | def resolve_attr(obj, attr): |
| 285 | for name in attr.split("."): |
| 286 | obj = getattr(obj, name) |
| 287 | return obj |
| 288 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 | |
Ezio Melotti | babc822 | 2013-05-08 10:53:11 +0300 | [diff] [blame] | 290 | .. function:: itemgetter(item) |
| 291 | itemgetter(*items) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 293 | Return a callable object that fetches *item* from its operand using the |
| 294 | operand's :meth:`__getitem__` method. If multiple items are specified, |
Ezio Melotti | babc822 | 2013-05-08 10:53:11 +0300 | [diff] [blame] | 295 | returns a tuple of lookup values. For example: |
| 296 | |
| 297 | * After ``f = itemgetter(2)``, the call ``f(r)`` returns ``r[2]``. |
| 298 | |
| 299 | * After ``g = itemgetter(2, 5, 3)``, the call ``g(r)`` returns |
| 300 | ``(r[2], r[5], r[3])``. |
| 301 | |
| 302 | Equivalent to:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 303 | |
Benjamin Peterson | ffec810 | 2010-08-21 20:01:28 +0000 | [diff] [blame] | 304 | def itemgetter(*items): |
| 305 | if len(items) == 1: |
| 306 | item = items[0] |
| 307 | def g(obj): |
| 308 | return obj[item] |
| 309 | else: |
| 310 | def g(obj): |
| 311 | return tuple(obj[item] for item in items) |
| 312 | return g |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 313 | |
| 314 | The items can be any type accepted by the operand's :meth:`__getitem__` |
| 315 | method. Dictionaries accept any hashable value. Lists, tuples, and |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 316 | strings accept an index or a slice: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 318 | >>> itemgetter(1)('ABCDEFG') |
| 319 | 'B' |
| 320 | >>> itemgetter(1,3,5)('ABCDEFG') |
| 321 | ('B', 'D', 'F') |
| 322 | >>> itemgetter(slice(2,None))('ABCDEFG') |
| 323 | 'CDEFG' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 324 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 325 | |
| 326 | Example of using :func:`itemgetter` to retrieve specific fields from a |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 327 | tuple record: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 328 | |
Benjamin Peterson | c16f8b3 | 2010-08-21 20:03:15 +0000 | [diff] [blame] | 329 | >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] |
| 330 | >>> getcount = itemgetter(1) |
Raymond Hettinger | d292a17 | 2010-09-01 07:46:54 +0000 | [diff] [blame] | 331 | >>> list(map(getcount, inventory)) |
Benjamin Peterson | c16f8b3 | 2010-08-21 20:03:15 +0000 | [diff] [blame] | 332 | [3, 2, 5, 1] |
| 333 | >>> sorted(inventory, key=getcount) |
| 334 | [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 335 | |
| 336 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 337 | .. function:: methodcaller(name[, args...]) |
| 338 | |
| 339 | Return a callable object that calls the method *name* on its operand. If |
| 340 | additional arguments and/or keyword arguments are given, they will be given |
Ezio Melotti | babc822 | 2013-05-08 10:53:11 +0300 | [diff] [blame] | 341 | to the method as well. For example: |
| 342 | |
| 343 | * After ``f = methodcaller('name')``, the call ``f(b)`` returns ``b.name()``. |
| 344 | |
| 345 | * After ``f = methodcaller('name', 'foo', bar=1)``, the call ``f(b)`` |
| 346 | returns ``b.name('foo', bar=1)``. |
| 347 | |
| 348 | Equivalent to:: |
Benjamin Peterson | 2d55e2a | 2010-08-21 20:08:36 +0000 | [diff] [blame] | 349 | |
| 350 | def methodcaller(name, *args, **kwargs): |
| 351 | def caller(obj): |
| 352 | return getattr(obj, name)(*args, **kwargs) |
| 353 | return caller |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 354 | |
| 355 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 356 | .. _operator-map: |
| 357 | |
| 358 | Mapping Operators to Functions |
| 359 | ------------------------------ |
| 360 | |
| 361 | This table shows how abstract operations correspond to operator symbols in the |
| 362 | Python syntax and the functions in the :mod:`operator` module. |
| 363 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 364 | +-----------------------+-------------------------+---------------------------------------+ |
| 365 | | Operation | Syntax | Function | |
| 366 | +=======================+=========================+=======================================+ |
| 367 | | Addition | ``a + b`` | ``add(a, b)`` | |
| 368 | +-----------------------+-------------------------+---------------------------------------+ |
| 369 | | Concatenation | ``seq1 + seq2`` | ``concat(seq1, seq2)`` | |
| 370 | +-----------------------+-------------------------+---------------------------------------+ |
| 371 | | Containment Test | ``obj in seq`` | ``contains(seq, obj)`` | |
| 372 | +-----------------------+-------------------------+---------------------------------------+ |
Sandro Tosi | 83c4882 | 2012-02-28 22:28:28 +0100 | [diff] [blame] | 373 | | Division | ``a / b`` | ``truediv(a, b)`` | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 374 | +-----------------------+-------------------------+---------------------------------------+ |
| 375 | | Division | ``a // b`` | ``floordiv(a, b)`` | |
| 376 | +-----------------------+-------------------------+---------------------------------------+ |
| 377 | | Bitwise And | ``a & b`` | ``and_(a, b)`` | |
| 378 | +-----------------------+-------------------------+---------------------------------------+ |
| 379 | | Bitwise Exclusive Or | ``a ^ b`` | ``xor(a, b)`` | |
| 380 | +-----------------------+-------------------------+---------------------------------------+ |
| 381 | | Bitwise Inversion | ``~ a`` | ``invert(a)`` | |
| 382 | +-----------------------+-------------------------+---------------------------------------+ |
| 383 | | Bitwise Or | ``a | b`` | ``or_(a, b)`` | |
| 384 | +-----------------------+-------------------------+---------------------------------------+ |
| 385 | | Exponentiation | ``a ** b`` | ``pow(a, b)`` | |
| 386 | +-----------------------+-------------------------+---------------------------------------+ |
| 387 | | Identity | ``a is b`` | ``is_(a, b)`` | |
| 388 | +-----------------------+-------------------------+---------------------------------------+ |
| 389 | | Identity | ``a is not b`` | ``is_not(a, b)`` | |
| 390 | +-----------------------+-------------------------+---------------------------------------+ |
| 391 | | Indexed Assignment | ``obj[k] = v`` | ``setitem(obj, k, v)`` | |
| 392 | +-----------------------+-------------------------+---------------------------------------+ |
| 393 | | Indexed Deletion | ``del obj[k]`` | ``delitem(obj, k)`` | |
| 394 | +-----------------------+-------------------------+---------------------------------------+ |
| 395 | | Indexing | ``obj[k]`` | ``getitem(obj, k)`` | |
| 396 | +-----------------------+-------------------------+---------------------------------------+ |
| 397 | | Left Shift | ``a << b`` | ``lshift(a, b)`` | |
| 398 | +-----------------------+-------------------------+---------------------------------------+ |
| 399 | | Modulo | ``a % b`` | ``mod(a, b)`` | |
| 400 | +-----------------------+-------------------------+---------------------------------------+ |
| 401 | | Multiplication | ``a * b`` | ``mul(a, b)`` | |
| 402 | +-----------------------+-------------------------+---------------------------------------+ |
| 403 | | Negation (Arithmetic) | ``- a`` | ``neg(a)`` | |
| 404 | +-----------------------+-------------------------+---------------------------------------+ |
| 405 | | Negation (Logical) | ``not a`` | ``not_(a)`` | |
| 406 | +-----------------------+-------------------------+---------------------------------------+ |
| 407 | | Positive | ``+ a`` | ``pos(a)`` | |
| 408 | +-----------------------+-------------------------+---------------------------------------+ |
| 409 | | Right Shift | ``a >> b`` | ``rshift(a, b)`` | |
| 410 | +-----------------------+-------------------------+---------------------------------------+ |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 411 | | Slice Assignment | ``seq[i:j] = values`` | ``setitem(seq, slice(i, j), values)`` | |
| 412 | +-----------------------+-------------------------+---------------------------------------+ |
| 413 | | Slice Deletion | ``del seq[i:j]`` | ``delitem(seq, slice(i, j))`` | |
| 414 | +-----------------------+-------------------------+---------------------------------------+ |
| 415 | | Slicing | ``seq[i:j]`` | ``getitem(seq, slice(i, j))`` | |
| 416 | +-----------------------+-------------------------+---------------------------------------+ |
| 417 | | String Formatting | ``s % obj`` | ``mod(s, obj)`` | |
| 418 | +-----------------------+-------------------------+---------------------------------------+ |
| 419 | | Subtraction | ``a - b`` | ``sub(a, b)`` | |
| 420 | +-----------------------+-------------------------+---------------------------------------+ |
| 421 | | Truth Test | ``obj`` | ``truth(obj)`` | |
| 422 | +-----------------------+-------------------------+---------------------------------------+ |
| 423 | | Ordering | ``a < b`` | ``lt(a, b)`` | |
| 424 | +-----------------------+-------------------------+---------------------------------------+ |
| 425 | | Ordering | ``a <= b`` | ``le(a, b)`` | |
| 426 | +-----------------------+-------------------------+---------------------------------------+ |
| 427 | | Equality | ``a == b`` | ``eq(a, b)`` | |
| 428 | +-----------------------+-------------------------+---------------------------------------+ |
| 429 | | Difference | ``a != b`` | ``ne(a, b)`` | |
| 430 | +-----------------------+-------------------------+---------------------------------------+ |
| 431 | | Ordering | ``a >= b`` | ``ge(a, b)`` | |
| 432 | +-----------------------+-------------------------+---------------------------------------+ |
| 433 | | Ordering | ``a > b`` | ``gt(a, b)`` | |
| 434 | +-----------------------+-------------------------+---------------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 435 | |
Raymond Hettinger | 83b1ab0 | 2011-01-08 10:26:53 +0000 | [diff] [blame] | 436 | Inplace Operators |
Sandro Tosi | 3f7d1d3 | 2012-06-01 20:23:20 +0200 | [diff] [blame] | 437 | ----------------- |
Raymond Hettinger | 83b1ab0 | 2011-01-08 10:26:53 +0000 | [diff] [blame] | 438 | |
| 439 | Many operations have an "in-place" version. Listed below are functions |
| 440 | providing a more primitive access to in-place operators than the usual syntax |
| 441 | does; for example, the :term:`statement` ``x += y`` is equivalent to |
| 442 | ``x = operator.iadd(x, y)``. Another way to put it is to say that |
| 443 | ``z = operator.iadd(x, y)`` is equivalent to the compound statement |
| 444 | ``z = x; z += y``. |
| 445 | |
| 446 | In those examples, note that when an in-place method is called, the computation |
| 447 | and assignment are performed in two separate steps. The in-place functions |
| 448 | listed below only do the first step, calling the in-place method. The second |
| 449 | step, assignment, is not handled. |
| 450 | |
| 451 | For immutable targets such as strings, numbers, and tuples, the updated |
| 452 | value is computed, but not assigned back to the input variable: |
| 453 | |
| 454 | >>> a = 'hello' |
| 455 | >>> iadd(a, ' world') |
| 456 | 'hello world' |
| 457 | >>> a |
| 458 | 'hello' |
| 459 | |
| 460 | For mutable targets such as lists and dictionaries, the inplace method |
| 461 | will perform the update, so no subsequent assignment is necessary: |
| 462 | |
| 463 | >>> s = ['h', 'e', 'l', 'l', 'o'] |
| 464 | >>> iadd(s, [' ', 'w', 'o', 'r', 'l', 'd']) |
| 465 | ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] |
| 466 | >>> s |
| 467 | ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] |
| 468 | |
| 469 | .. function:: iadd(a, b) |
| 470 | __iadd__(a, b) |
| 471 | |
| 472 | ``a = iadd(a, b)`` is equivalent to ``a += b``. |
| 473 | |
| 474 | |
| 475 | .. function:: iand(a, b) |
| 476 | __iand__(a, b) |
| 477 | |
| 478 | ``a = iand(a, b)`` is equivalent to ``a &= b``. |
| 479 | |
| 480 | |
| 481 | .. function:: iconcat(a, b) |
| 482 | __iconcat__(a, b) |
| 483 | |
| 484 | ``a = iconcat(a, b)`` is equivalent to ``a += b`` for *a* and *b* sequences. |
| 485 | |
| 486 | |
| 487 | .. function:: ifloordiv(a, b) |
| 488 | __ifloordiv__(a, b) |
| 489 | |
| 490 | ``a = ifloordiv(a, b)`` is equivalent to ``a //= b``. |
| 491 | |
| 492 | |
| 493 | .. function:: ilshift(a, b) |
| 494 | __ilshift__(a, b) |
| 495 | |
| 496 | ``a = ilshift(a, b)`` is equivalent to ``a <<= b``. |
| 497 | |
| 498 | |
| 499 | .. function:: imod(a, b) |
| 500 | __imod__(a, b) |
| 501 | |
| 502 | ``a = imod(a, b)`` is equivalent to ``a %= b``. |
| 503 | |
| 504 | |
| 505 | .. function:: imul(a, b) |
| 506 | __imul__(a, b) |
| 507 | |
| 508 | ``a = imul(a, b)`` is equivalent to ``a *= b``. |
| 509 | |
| 510 | |
| 511 | .. function:: ior(a, b) |
| 512 | __ior__(a, b) |
| 513 | |
| 514 | ``a = ior(a, b)`` is equivalent to ``a |= b``. |
| 515 | |
| 516 | |
| 517 | .. function:: ipow(a, b) |
| 518 | __ipow__(a, b) |
| 519 | |
| 520 | ``a = ipow(a, b)`` is equivalent to ``a **= b``. |
| 521 | |
| 522 | |
| 523 | .. function:: irshift(a, b) |
| 524 | __irshift__(a, b) |
| 525 | |
| 526 | ``a = irshift(a, b)`` is equivalent to ``a >>= b``. |
| 527 | |
| 528 | |
| 529 | .. function:: isub(a, b) |
| 530 | __isub__(a, b) |
| 531 | |
| 532 | ``a = isub(a, b)`` is equivalent to ``a -= b``. |
| 533 | |
| 534 | |
| 535 | .. function:: itruediv(a, b) |
| 536 | __itruediv__(a, b) |
| 537 | |
| 538 | ``a = itruediv(a, b)`` is equivalent to ``a /= b``. |
| 539 | |
| 540 | |
| 541 | .. function:: ixor(a, b) |
| 542 | __ixor__(a, b) |
| 543 | |
| 544 | ``a = ixor(a, b)`` is equivalent to ``a ^= b``. |