blob: 9a613abf61045da0ed8ad0e058981aa598f450d7 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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
10The :mod:`operator` module exports a set of functions implemented in C
11corresponding to the intrinsic operators of Python. For example,
12``operator.add(x, y)`` is equivalent to the expression ``x+y``. The function
13names are those used for special class methods; variants without leading and
14trailing ``__`` are also provided for convenience.
15
16The functions fall into categories that perform object comparisons, logical
17operations, mathematical operations, sequence operations, and abstract type
18tests.
19
20The object comparison functions are useful for all objects, and are named after
21the 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 Brandl116aa622007-08-15 14:28:22 +000045
46The logical operations are also generally applicable to all objects, and support
47truth tests, identity tests, and boolean operations:
48
49
Thomas Wouters1b7f8912007-09-19 03:06:30 +000050.. function:: not_(obj)
51 __not__(obj)
Georg Brandl116aa622007-08-15 14:28:22 +000052
Thomas Wouters1b7f8912007-09-19 03:06:30 +000053 Return the outcome of :keyword:`not` *obj*. (Note that there is no
Georg Brandl116aa622007-08-15 14:28:22 +000054 :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 Wouters1b7f8912007-09-19 03:06:30 +000059.. function:: truth(obj)
Georg Brandl116aa622007-08-15 14:28:22 +000060
Thomas Wouters1b7f8912007-09-19 03:06:30 +000061 Return :const:`True` if *obj* is true, and :const:`False` otherwise. This is
Georg Brandl116aa622007-08-15 14:28:22 +000062 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 Brandl116aa622007-08-15 14:28:22 +000069
70.. function:: is_not(a, b)
71
72 Return ``a is not b``. Tests object identity.
73
Georg Brandl116aa622007-08-15 14:28:22 +000074
75The mathematical and bitwise operations are the most numerous:
76
77
Thomas Wouters1b7f8912007-09-19 03:06:30 +000078.. function:: abs(obj)
79 __abs__(obj)
Georg Brandl116aa622007-08-15 14:28:22 +000080
Thomas Wouters1b7f8912007-09-19 03:06:30 +000081 Return the absolute value of *obj*.
Georg Brandl116aa622007-08-15 14:28:22 +000082
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 Brandl116aa622007-08-15 14:28:22 +000096.. function:: floordiv(a, b)
97 __floordiv__(a, b)
98
99 Return ``a // b``.
100
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000102.. function:: inv(obj)
103 invert(obj)
104 __inv__(obj)
105 __invert__(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000107 Return the bitwise inverse of the number *obj*. This is equivalent to ``~obj``.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Georg Brandl116aa622007-08-15 14:28:22 +0000109
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 Wouters1b7f8912007-09-19 03:06:30 +0000128.. function:: neg(obj)
129 __neg__(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000131 Return *obj* negated.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133
134.. function:: or_(a, b)
135 __or__(a, b)
136
137 Return the bitwise or of *a* and *b*.
138
139
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000140.. function:: pos(obj)
141 __pos__(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000143 Return *obj* positive.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145
146.. function:: pow(a, b)
147 __pow__(a, b)
148
149 Return ``a ** b``, for *a* and *b* numbers.
150
Georg Brandl116aa622007-08-15 14:28:22 +0000151
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 Brandlf6945182008-02-01 11:56:49 +0000167 Return ``a / b`` where 2/3 is .66 rather than 0. This is also known as
168 "true" division.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Georg Brandl116aa622007-08-15 14:28:22 +0000170
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 Brandl116aa622007-08-15 14:28:22 +0000182
183Operations 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 Brandl116aa622007-08-15 14:28:22 +0000196
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 Brandlf6945182008-02-01 11:56:49 +0000207
Georg Brandl116aa622007-08-15 14:28:22 +0000208.. 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 Brandl116aa622007-08-15 14:28:22 +0000237.. 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 Brandlf6945182008-02-01 11:56:49 +0000248
Georg Brandl116aa622007-08-15 14:28:22 +0000249Many operations have an "in-place" version. The following functions provide a
250more primitive access to in-place operators than the usual syntax does; for
Christian Heimesd8654cf2007-12-02 15:22:16 +0000251example, 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 Brandl116aa622007-08-15 14:28:22 +0000255
256.. function:: iadd(a, b)
257 __iadd__(a, b)
258
259 ``a = iadd(a, b)`` is equivalent to ``a += b``.
260
Georg Brandl116aa622007-08-15 14:28:22 +0000261
262.. function:: iand(a, b)
263 __iand__(a, b)
264
265 ``a = iand(a, b)`` is equivalent to ``a &= b``.
266
Georg Brandl116aa622007-08-15 14:28:22 +0000267
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 Brandl116aa622007-08-15 14:28:22 +0000273
Georg Brandl116aa622007-08-15 14:28:22 +0000274.. function:: ifloordiv(a, b)
275 __ifloordiv__(a, b)
276
277 ``a = ifloordiv(a, b)`` is equivalent to ``a //= b``.
278
Georg Brandl116aa622007-08-15 14:28:22 +0000279
280.. function:: ilshift(a, b)
281 __ilshift__(a, b)
282
Georg Brandl55ac8f02007-09-01 13:51:09 +0000283 ``a = ilshift(a, b)`` is equivalent to ``a <<= b``.
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285
286.. function:: imod(a, b)
287 __imod__(a, b)
288
289 ``a = imod(a, b)`` is equivalent to ``a %= b``.
290
Georg Brandl116aa622007-08-15 14:28:22 +0000291
292.. function:: imul(a, b)
293 __imul__(a, b)
294
295 ``a = imul(a, b)`` is equivalent to ``a *= b``.
296
Georg Brandl116aa622007-08-15 14:28:22 +0000297
298.. function:: ior(a, b)
299 __ior__(a, b)
300
301 ``a = ior(a, b)`` is equivalent to ``a |= b``.
302
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304.. function:: ipow(a, b)
305 __ipow__(a, b)
306
307 ``a = ipow(a, b)`` is equivalent to ``a **= b``.
308
Georg Brandl116aa622007-08-15 14:28:22 +0000309
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 Brandl116aa622007-08-15 14:28:22 +0000316
317.. function:: irshift(a, b)
318 __irshift__(a, b)
319
320 ``a = irshift(a, b)`` is equivalent to ``a >>= b``.
321
Georg Brandl116aa622007-08-15 14:28:22 +0000322
323.. function:: isub(a, b)
324 __isub__(a, b)
325
326 ``a = isub(a, b)`` is equivalent to ``a -= b``.
327
Georg Brandl116aa622007-08-15 14:28:22 +0000328
329.. function:: itruediv(a, b)
330 __itruediv__(a, b)
331
Georg Brandlf6945182008-02-01 11:56:49 +0000332 ``a = itruediv(a, b)`` is equivalent to ``a /= b``.
Georg Brandl116aa622007-08-15 14:28:22 +0000333
Georg Brandl116aa622007-08-15 14:28:22 +0000334
335.. function:: ixor(a, b)
336 __ixor__(a, b)
337
338 ``a = ixor(a, b)`` is equivalent to ``a ^= b``.
339
Georg Brandl116aa622007-08-15 14:28:22 +0000340
341The :mod:`operator` module also defines a few predicates to test the type of
342objects.
343
Georg Brandlf6945182008-02-01 11:56:49 +0000344.. XXX just remove them?
Georg Brandl116aa622007-08-15 14:28:22 +0000345.. note::
346
Georg Brandlf6945182008-02-01 11:56:49 +0000347 Be careful not to misinterpret the results of these functions; none have any
348 measure of reliability with instance objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000349 For example::
350
351 >>> class C:
352 ... pass
353 ...
354 >>> import operator
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000355 >>> obj = C()
356 >>> operator.isMappingType(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000357 True
358
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000359.. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000360
Georg Brandlf6945182008-02-01 11:56:49 +0000361 Since there are now abstract classes for collection types, you should write,
362 for example, ``isinstance(obj, collections.Mapping)`` and ``isinstance(obj,
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000363 collections.Sequence)``.
364
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000365.. function:: isMappingType(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000366
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000367 Returns true if the object *obj* supports the mapping interface. This is true for
Georg Brandl116aa622007-08-15 14:28:22 +0000368 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 Wouters1b7f8912007-09-19 03:06:30 +0000377.. function:: isNumberType(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000378
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000379 Returns true if the object *obj* represents a number. This is true for all
Georg Brandl116aa622007-08-15 14:28:22 +0000380 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 Wouters1b7f8912007-09-19 03:06:30 +0000389.. function:: isSequenceType(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000390
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000391 Returns true if the object *obj* supports the sequence protocol. This returns true
Georg Brandl116aa622007-08-15 14:28:22 +0000392 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
401Example: Build a dictionary that maps the ordinals from ``0`` to ``255`` to
402their 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
412The :mod:`operator` module also defines tools for generalized attribute and item
413lookups. These are useful for making fast field extractors as arguments for
414:func:`map`, :func:`sorted`, :meth:`itertools.groupby`, or other functions that
415expect 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,
422 ``f=attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After,
423 ``f=attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name,
424 b.date)``.
425
Georg Brandl116aa622007-08-15 14:28:22 +0000426
427.. function:: itemgetter(item[, args...])
428
429 Return a callable object that fetches *item* from its operand. If more than one
430 item is requested, returns a tuple of items. After, ``f=itemgetter(2)``, the
431 call ``f(b)`` returns ``b[2]``. After, ``f=itemgetter(2,5,3)``, the call
432 ``f(b)`` returns ``(b[2], b[5], b[3])``.
433
Georg Brandl116aa622007-08-15 14:28:22 +0000434
435Examples::
436
437 >>> from operator import itemgetter
438 >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
439 >>> getcount = itemgetter(1)
440 >>> map(getcount, inventory)
441 [3, 2, 5, 1]
442 >>> sorted(inventory, key=getcount)
443 [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]
444
445
446.. _operator-map:
447
448Mapping Operators to Functions
449------------------------------
450
451This table shows how abstract operations correspond to operator symbols in the
452Python syntax and the functions in the :mod:`operator` module.
453
454+-----------------------+-------------------------+---------------------------------+
455| Operation | Syntax | Function |
456+=======================+=========================+=================================+
457| Addition | ``a + b`` | ``add(a, b)`` |
458+-----------------------+-------------------------+---------------------------------+
459| Concatenation | ``seq1 + seq2`` | ``concat(seq1, seq2)`` |
460+-----------------------+-------------------------+---------------------------------+
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000461| Containment Test | ``obj in seq`` | ``contains(seq, obj)`` |
Georg Brandl116aa622007-08-15 14:28:22 +0000462+-----------------------+-------------------------+---------------------------------+
Georg Brandlf6945182008-02-01 11:56:49 +0000463| Division | ``a / b`` | ``truediv(a, b)`` |
Georg Brandl116aa622007-08-15 14:28:22 +0000464+-----------------------+-------------------------+---------------------------------+
465| Division | ``a // b`` | ``floordiv(a, b)`` |
466+-----------------------+-------------------------+---------------------------------+
467| Bitwise And | ``a & b`` | ``and_(a, b)`` |
468+-----------------------+-------------------------+---------------------------------+
469| Bitwise Exclusive Or | ``a ^ b`` | ``xor(a, b)`` |
470+-----------------------+-------------------------+---------------------------------+
471| Bitwise Inversion | ``~ a`` | ``invert(a)`` |
472+-----------------------+-------------------------+---------------------------------+
473| Bitwise Or | ``a | b`` | ``or_(a, b)`` |
474+-----------------------+-------------------------+---------------------------------+
475| Exponentiation | ``a ** b`` | ``pow(a, b)`` |
476+-----------------------+-------------------------+---------------------------------+
477| Identity | ``a is b`` | ``is_(a, b)`` |
478+-----------------------+-------------------------+---------------------------------+
479| Identity | ``a is not b`` | ``is_not(a, b)`` |
480+-----------------------+-------------------------+---------------------------------+
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000481| Indexed Assignment | ``obj[k] = v`` | ``setitem(obj, k, v)`` |
Georg Brandl116aa622007-08-15 14:28:22 +0000482+-----------------------+-------------------------+---------------------------------+
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000483| Indexed Deletion | ``del obj[k]`` | ``delitem(obj, k)`` |
Georg Brandl116aa622007-08-15 14:28:22 +0000484+-----------------------+-------------------------+---------------------------------+
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000485| Indexing | ``obj[k]`` | ``getitem(obj, k)`` |
Georg Brandl116aa622007-08-15 14:28:22 +0000486+-----------------------+-------------------------+---------------------------------+
487| Left Shift | ``a << b`` | ``lshift(a, b)`` |
488+-----------------------+-------------------------+---------------------------------+
489| Modulo | ``a % b`` | ``mod(a, b)`` |
490+-----------------------+-------------------------+---------------------------------+
491| Multiplication | ``a * b`` | ``mul(a, b)`` |
492+-----------------------+-------------------------+---------------------------------+
493| Negation (Arithmetic) | ``- a`` | ``neg(a)`` |
494+-----------------------+-------------------------+---------------------------------+
495| Negation (Logical) | ``not a`` | ``not_(a)`` |
496+-----------------------+-------------------------+---------------------------------+
497| Right Shift | ``a >> b`` | ``rshift(a, b)`` |
498+-----------------------+-------------------------+---------------------------------+
499| Sequence Repitition | ``seq * i`` | ``repeat(seq, i)`` |
500+-----------------------+-------------------------+---------------------------------+
501| Slice Assignment | ``seq[i:j] = values`` | ``setslice(seq, i, j, values)`` |
502+-----------------------+-------------------------+---------------------------------+
503| Slice Deletion | ``del seq[i:j]`` | ``delslice(seq, i, j)`` |
504+-----------------------+-------------------------+---------------------------------+
505| Slicing | ``seq[i:j]`` | ``getslice(seq, i, j)`` |
506+-----------------------+-------------------------+---------------------------------+
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000507| String Formatting | ``s % obj`` | ``mod(s, obj)`` |
Georg Brandl116aa622007-08-15 14:28:22 +0000508+-----------------------+-------------------------+---------------------------------+
509| Subtraction | ``a - b`` | ``sub(a, b)`` |
510+-----------------------+-------------------------+---------------------------------+
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000511| Truth Test | ``obj`` | ``truth(obj)`` |
Georg Brandl116aa622007-08-15 14:28:22 +0000512+-----------------------+-------------------------+---------------------------------+
513| Ordering | ``a < b`` | ``lt(a, b)`` |
514+-----------------------+-------------------------+---------------------------------+
515| Ordering | ``a <= b`` | ``le(a, b)`` |
516+-----------------------+-------------------------+---------------------------------+
517| Equality | ``a == b`` | ``eq(a, b)`` |
518+-----------------------+-------------------------+---------------------------------+
519| Difference | ``a != b`` | ``ne(a, b)`` |
520+-----------------------+-------------------------+---------------------------------+
521| Ordering | ``a >= b`` | ``ge(a, b)`` |
522+-----------------------+-------------------------+---------------------------------+
523| Ordering | ``a > b`` | ``gt(a, b)`` |
524+-----------------------+-------------------------+---------------------------------+
525