blob: dc7ad37da57e98d6c049b58314331fb0e7649444 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Georg Brandl61854332008-03-23 08:05:30 +00009.. testsetup::
Georg Brandlc62ef8b2009-01-03 20:55:06 +000010
Georg Brandl61854332008-03-23 08:05:30 +000011 import operator
12 from operator import itemgetter
13
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
15The :mod:`operator` module exports a set of functions implemented in C
16corresponding to the intrinsic operators of Python. For example,
17``operator.add(x, y)`` is equivalent to the expression ``x+y``. The function
18names are those used for special class methods; variants without leading and
19trailing ``__`` are also provided for convenience.
20
21The functions fall into categories that perform object comparisons, logical
22operations, mathematical operations, sequence operations, and abstract type
23tests.
24
25The object comparison functions are useful for all objects, and are named after
26the 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
46 >= b``. Note that unlike the built-in :func:`cmp`, these functions can
47 return any value, which may or may not be interpretable as a Boolean value.
48 See :ref:`comparisons` for more information about rich comparisons.
49
50 .. versionadded:: 2.2
51
52The logical operations are also generally applicable to all objects, and support
53truth tests, identity tests, and boolean operations:
54
55
Mark Summerfieldddca9f02007-09-13 14:54:30 +000056.. function:: not_(obj)
57 __not__(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
Mark Summerfieldddca9f02007-09-13 14:54:30 +000059 Return the outcome of :keyword:`not` *obj*. (Note that there is no
Georg Brandl8ec7f652007-08-15 14:28:01 +000060 :meth:`__not__` method for object instances; only the interpreter core defines
61 this operation. The result is affected by the :meth:`__nonzero__` and
62 :meth:`__len__` methods.)
63
64
Mark Summerfieldddca9f02007-09-13 14:54:30 +000065.. function:: truth(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +000066
Mark Summerfieldddca9f02007-09-13 14:54:30 +000067 Return :const:`True` if *obj* is true, and :const:`False` otherwise. This is
Georg Brandl8ec7f652007-08-15 14:28:01 +000068 equivalent to using the :class:`bool` constructor.
69
70
71.. function:: is_(a, b)
72
73 Return ``a is b``. Tests object identity.
74
75 .. versionadded:: 2.3
76
77
78.. function:: is_not(a, b)
79
80 Return ``a is not b``. Tests object identity.
81
82 .. versionadded:: 2.3
83
84The mathematical and bitwise operations are the most numerous:
85
86
Mark Summerfieldddca9f02007-09-13 14:54:30 +000087.. function:: abs(obj)
88 __abs__(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +000089
Mark Summerfieldddca9f02007-09-13 14:54:30 +000090 Return the absolute value of *obj*.
Georg Brandl8ec7f652007-08-15 14:28:01 +000091
92
93.. function:: add(a, b)
94 __add__(a, b)
95
96 Return ``a + b``, for *a* and *b* numbers.
97
98
99.. function:: and_(a, b)
100 __and__(a, b)
101
102 Return the bitwise and of *a* and *b*.
103
104
105.. function:: div(a, b)
106 __div__(a, b)
107
108 Return ``a / b`` when ``__future__.division`` is not in effect. This is
109 also known as "classic" division.
110
111
112.. function:: floordiv(a, b)
113 __floordiv__(a, b)
114
115 Return ``a // b``.
116
117 .. versionadded:: 2.2
118
119
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000120.. function:: inv(obj)
121 invert(obj)
122 __inv__(obj)
123 __invert__(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000125 Return the bitwise inverse of the number *obj*. This is equivalent to ``~obj``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000126
127 .. versionadded:: 2.0
128 The names :func:`invert` and :func:`__invert__`.
129
130
131.. function:: lshift(a, b)
132 __lshift__(a, b)
133
134 Return *a* shifted left by *b*.
135
136
137.. function:: mod(a, b)
138 __mod__(a, b)
139
140 Return ``a % b``.
141
142
143.. function:: mul(a, b)
144 __mul__(a, b)
145
146 Return ``a * b``, for *a* and *b* numbers.
147
148
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000149.. function:: neg(obj)
150 __neg__(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000151
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000152 Return *obj* negated.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000153
154
155.. function:: or_(a, b)
156 __or__(a, b)
157
158 Return the bitwise or of *a* and *b*.
159
160
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000161.. function:: pos(obj)
162 __pos__(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000163
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000164 Return *obj* positive.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000165
166
167.. function:: pow(a, b)
168 __pow__(a, b)
169
170 Return ``a ** b``, for *a* and *b* numbers.
171
172 .. versionadded:: 2.3
173
174
175.. function:: rshift(a, b)
176 __rshift__(a, b)
177
178 Return *a* shifted right by *b*.
179
180
181.. function:: sub(a, b)
182 __sub__(a, b)
183
184 Return ``a - b``.
185
186
187.. function:: truediv(a, b)
188 __truediv__(a, b)
189
190 Return ``a / b`` when ``__future__.division`` is in effect. This is also
191 known as "true" division.
192
193 .. versionadded:: 2.2
194
195
196.. function:: xor(a, b)
197 __xor__(a, b)
198
199 Return the bitwise exclusive or of *a* and *b*.
200
201
202.. function:: index(a)
203 __index__(a)
204
205 Return *a* converted to an integer. Equivalent to ``a.__index__()``.
206
207 .. versionadded:: 2.5
208
209
210Operations which work with sequences include:
211
212.. function:: concat(a, b)
213 __concat__(a, b)
214
215 Return ``a + b`` for *a* and *b* sequences.
216
217
218.. function:: contains(a, b)
219 __contains__(a, b)
220
221 Return the outcome of the test ``b in a``. Note the reversed operands.
222
223 .. versionadded:: 2.0
224 The name :func:`__contains__`.
225
226
227.. function:: countOf(a, b)
228
229 Return the number of occurrences of *b* in *a*.
230
231
232.. function:: delitem(a, b)
233 __delitem__(a, b)
234
235 Remove the value of *a* at index *b*.
236
237
238.. function:: delslice(a, b, c)
239 __delslice__(a, b, c)
240
241 Delete the slice of *a* from index *b* to index *c-1*.
242
Georg Brandl95a840b2008-12-05 15:42:03 +0000243 .. deprecated:: 2.6
244 This function is removed in Python 3.x. Use :func:`delitem` with a slice
245 index.
246
Georg Brandl8ec7f652007-08-15 14:28:01 +0000247
248.. function:: getitem(a, b)
249 __getitem__(a, b)
250
251 Return the value of *a* at index *b*.
252
253
254.. function:: getslice(a, b, c)
255 __getslice__(a, b, c)
256
257 Return the slice of *a* from index *b* to index *c-1*.
258
Georg Brandl95a840b2008-12-05 15:42:03 +0000259 .. deprecated:: 2.6
260 This function is removed in Python 3.x. Use :func:`getitem` with a slice
261 index.
262
Georg Brandl8ec7f652007-08-15 14:28:01 +0000263
264.. function:: indexOf(a, b)
265
266 Return the index of the first of occurrence of *b* in *a*.
267
268
269.. function:: repeat(a, b)
270 __repeat__(a, b)
271
272 Return ``a * b`` where *a* is a sequence and *b* is an integer.
273
274
275.. function:: sequenceIncludes(...)
276
277 .. deprecated:: 2.0
278 Use :func:`contains` instead.
279
280 Alias for :func:`contains`.
281
282
283.. function:: setitem(a, b, c)
284 __setitem__(a, b, c)
285
286 Set the value of *a* at index *b* to *c*.
287
288
289.. function:: setslice(a, b, c, v)
290 __setslice__(a, b, c, v)
291
292 Set the slice of *a* from index *b* to index *c-1* to the sequence *v*.
293
Georg Brandl95a840b2008-12-05 15:42:03 +0000294 .. deprecated:: 2.6
295 This function is removed in Python 3.x. Use :func:`setitem` with a slice
296 index.
297
298
Georg Brandl8ec7f652007-08-15 14:28:01 +0000299Many operations have an "in-place" version. The following functions provide a
300more primitive access to in-place operators than the usual syntax does; for
Georg Brandl584265b2007-12-02 14:58:50 +0000301example, the :term:`statement` ``x += y`` is equivalent to
302``x = operator.iadd(x, y)``. Another way to put it is to say that
303``z = operator.iadd(x, y)`` is equivalent to the compound statement
304``z = x; z += y``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000305
306.. function:: iadd(a, b)
307 __iadd__(a, b)
308
309 ``a = iadd(a, b)`` is equivalent to ``a += b``.
310
311 .. versionadded:: 2.5
312
313
314.. function:: iand(a, b)
315 __iand__(a, b)
316
317 ``a = iand(a, b)`` is equivalent to ``a &= b``.
318
319 .. versionadded:: 2.5
320
321
322.. function:: iconcat(a, b)
323 __iconcat__(a, b)
324
325 ``a = iconcat(a, b)`` is equivalent to ``a += b`` for *a* and *b* sequences.
326
327 .. versionadded:: 2.5
328
329
330.. function:: idiv(a, b)
331 __idiv__(a, b)
332
333 ``a = idiv(a, b)`` is equivalent to ``a /= b`` when ``__future__.division`` is
334 not in effect.
335
336 .. versionadded:: 2.5
337
338
339.. function:: ifloordiv(a, b)
340 __ifloordiv__(a, b)
341
342 ``a = ifloordiv(a, b)`` is equivalent to ``a //= b``.
343
344 .. versionadded:: 2.5
345
346
347.. function:: ilshift(a, b)
348 __ilshift__(a, b)
349
350 ``a = ilshift(a, b)`` is equivalent to ``a <``\ ``<= b``.
351
352 .. versionadded:: 2.5
353
354
355.. function:: imod(a, b)
356 __imod__(a, b)
357
358 ``a = imod(a, b)`` is equivalent to ``a %= b``.
359
360 .. versionadded:: 2.5
361
362
363.. function:: imul(a, b)
364 __imul__(a, b)
365
366 ``a = imul(a, b)`` is equivalent to ``a *= b``.
367
368 .. versionadded:: 2.5
369
370
371.. function:: ior(a, b)
372 __ior__(a, b)
373
374 ``a = ior(a, b)`` is equivalent to ``a |= b``.
375
376 .. versionadded:: 2.5
377
378
379.. function:: ipow(a, b)
380 __ipow__(a, b)
381
382 ``a = ipow(a, b)`` is equivalent to ``a **= b``.
383
384 .. versionadded:: 2.5
385
386
387.. function:: irepeat(a, b)
388 __irepeat__(a, b)
389
390 ``a = irepeat(a, b)`` is equivalent to ``a *= b`` where *a* is a sequence and
391 *b* is an integer.
392
393 .. versionadded:: 2.5
394
395
396.. function:: irshift(a, b)
397 __irshift__(a, b)
398
399 ``a = irshift(a, b)`` is equivalent to ``a >>= b``.
400
401 .. versionadded:: 2.5
402
403
404.. function:: isub(a, b)
405 __isub__(a, b)
406
407 ``a = isub(a, b)`` is equivalent to ``a -= b``.
408
409 .. versionadded:: 2.5
410
411
412.. function:: itruediv(a, b)
413 __itruediv__(a, b)
414
415 ``a = itruediv(a, b)`` is equivalent to ``a /= b`` when ``__future__.division``
416 is in effect.
417
418 .. versionadded:: 2.5
419
420
421.. function:: ixor(a, b)
422 __ixor__(a, b)
423
424 ``a = ixor(a, b)`` is equivalent to ``a ^= b``.
425
426 .. versionadded:: 2.5
427
428
429The :mod:`operator` module also defines a few predicates to test the type of
430objects.
431
432.. note::
433
434 Be careful not to misinterpret the results of these functions; only
435 :func:`isCallable` has any measure of reliability with instance objects.
Georg Brandl4f0f34f2008-03-22 21:26:44 +0000436 For example:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000437
438 >>> class C:
439 ... pass
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000440 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000441 >>> import operator
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000442 >>> obj = C()
443 >>> operator.isMappingType(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000444 True
445
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000446.. note::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000447
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000448 Python 3 is expected to introduce abstract base classes for
449 collection types, so it should be possible to write, for example,
450 ``isinstance(obj, collections.Mapping)`` and ``isinstance(obj,
451 collections.Sequence)``.
452
453.. function:: isCallable(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000454
455 .. deprecated:: 2.0
456 Use the :func:`callable` built-in function instead.
457
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000458 Returns true if the object *obj* can be called like a function, otherwise it
Georg Brandl8ec7f652007-08-15 14:28:01 +0000459 returns false. True is returned for functions, bound and unbound methods, class
460 objects, and instance objects which support the :meth:`__call__` method.
461
462
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000463.. function:: isMappingType(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000464
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000465 Returns true if the object *obj* supports the mapping interface. This is true for
Georg Brandl8ec7f652007-08-15 14:28:01 +0000466 dictionaries and all instance objects defining :meth:`__getitem__`.
467
468 .. warning::
469
470 There is no reliable way to test if an instance supports the complete mapping
471 protocol since the interface itself is ill-defined. This makes this test less
472 useful than it otherwise might be.
473
474
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000475.. function:: isNumberType(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000476
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000477 Returns true if the object *obj* represents a number. This is true for all
Georg Brandl8ec7f652007-08-15 14:28:01 +0000478 numeric types implemented in C.
479
480 .. warning::
481
482 There is no reliable way to test if an instance supports the complete numeric
483 interface since the interface itself is ill-defined. This makes this test less
484 useful than it otherwise might be.
485
486
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000487.. function:: isSequenceType(obj)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000488
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000489 Returns true if the object *obj* supports the sequence protocol. This returns true
Georg Brandl8ec7f652007-08-15 14:28:01 +0000490 for all objects which define sequence methods in C, and for all instance objects
491 defining :meth:`__getitem__`.
492
493 .. warning::
494
495 There is no reliable way to test if an instance supports the complete sequence
496 interface since the interface itself is ill-defined. This makes this test less
497 useful than it otherwise might be.
498
499Example: Build a dictionary that maps the ordinals from ``0`` to ``255`` to
Georg Brandl4f0f34f2008-03-22 21:26:44 +0000500their character equivalents.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000501
Georg Brandl8ec7f652007-08-15 14:28:01 +0000502 >>> d = {}
503 >>> keys = range(256)
504 >>> vals = map(chr, keys)
Georg Brandl61854332008-03-23 08:05:30 +0000505 >>> map(operator.setitem, [d]*len(keys), keys, vals) # doctest: +SKIP
Georg Brandl8ec7f652007-08-15 14:28:01 +0000506
507.. XXX: find a better, readable, example
508
509The :mod:`operator` module also defines tools for generalized attribute and item
510lookups. These are useful for making fast field extractors as arguments for
511:func:`map`, :func:`sorted`, :meth:`itertools.groupby`, or other functions that
512expect a function argument.
513
514
515.. function:: attrgetter(attr[, args...])
516
517 Return a callable object that fetches *attr* from its operand. If more than one
518 attribute is requested, returns a tuple of attributes. After,
Georg Brandle2065c62008-02-23 23:02:23 +0000519 ``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After,
520 ``f = attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000521 b.date)``.
522
Georg Brandle2065c62008-02-23 23:02:23 +0000523 The attribute names can also contain dots; after ``f = attrgetter('date.month')``,
524 the call ``f(b)`` returns ``b.date.month``.
525
Georg Brandl8ec7f652007-08-15 14:28:01 +0000526 .. versionadded:: 2.4
527
528 .. versionchanged:: 2.5
529 Added support for multiple attributes.
530
Georg Brandle2065c62008-02-23 23:02:23 +0000531 .. versionchanged:: 2.6
532 Added support for dotted attributes.
533
Georg Brandl8ec7f652007-08-15 14:28:01 +0000534
535.. function:: itemgetter(item[, args...])
536
Raymond Hettinger513460f2008-03-11 21:37:46 +0000537 Return a callable object that fetches *item* from its operand using the
538 operand's :meth:`__getitem__` method. If multiple items are specified,
539 returns a tuple of lookup values. Equivalent to::
540
541 def itemgetter(*items):
542 if len(items) == 1:
543 item = items[0]
544 def g(obj):
545 return obj[item]
546 else:
547 def g(obj):
548 return tuple(obj[item] for item in items)
549 return g
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000550
551 The items can be any type accepted by the operand's :meth:`__getitem__`
552 method. Dictionaries accept any hashable value. Lists, tuples, and
Georg Brandl4f0f34f2008-03-22 21:26:44 +0000553 strings accept an index or a slice:
Raymond Hettinger513460f2008-03-11 21:37:46 +0000554
Georg Brandl4f0f34f2008-03-22 21:26:44 +0000555 >>> itemgetter(1)('ABCDEFG')
556 'B'
557 >>> itemgetter(1,3,5)('ABCDEFG')
558 ('B', 'D', 'F')
559 >>> itemgetter(slice(2,None))('ABCDEFG')
560 'CDEFG'
Georg Brandl8ec7f652007-08-15 14:28:01 +0000561
562 .. versionadded:: 2.4
563
564 .. versionchanged:: 2.5
565 Added support for multiple item extraction.
566
Raymond Hettinger513460f2008-03-11 21:37:46 +0000567 Example of using :func:`itemgetter` to retrieve specific fields from a
Georg Brandl4f0f34f2008-03-22 21:26:44 +0000568 tuple record:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000569
Raymond Hettinger513460f2008-03-11 21:37:46 +0000570 >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
571 >>> getcount = itemgetter(1)
572 >>> map(getcount, inventory)
573 [3, 2, 5, 1]
574 >>> sorted(inventory, key=getcount)
575 [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000576
577
Georg Brandlebcfd112008-02-23 23:04:35 +0000578.. function:: methodcaller(name[, args...])
579
580 Return a callable object that calls the method *name* on its operand. If
581 additional arguments and/or keyword arguments are given, they will be given
582 to the method as well. After ``f = methodcaller('name')``, the call ``f(b)``
583 returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the
584 call ``f(b)`` returns ``b.name('foo', bar=1)``.
585
586 .. versionadded:: 2.6
587
588
Georg Brandl8ec7f652007-08-15 14:28:01 +0000589.. _operator-map:
590
591Mapping Operators to Functions
592------------------------------
593
594This table shows how abstract operations correspond to operator symbols in the
595Python syntax and the functions in the :mod:`operator` module.
596
597+-----------------------+-------------------------+---------------------------------+
598| Operation | Syntax | Function |
599+=======================+=========================+=================================+
600| Addition | ``a + b`` | ``add(a, b)`` |
601+-----------------------+-------------------------+---------------------------------+
602| Concatenation | ``seq1 + seq2`` | ``concat(seq1, seq2)`` |
603+-----------------------+-------------------------+---------------------------------+
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000604| Containment Test | ``obj in seq`` | ``contains(seq, obj)`` |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000605+-----------------------+-------------------------+---------------------------------+
606| Division | ``a / b`` | ``div(a, b)`` (without |
607| | | ``__future__.division``) |
608+-----------------------+-------------------------+---------------------------------+
609| Division | ``a / b`` | ``truediv(a, b)`` (with |
610| | | ``__future__.division``) |
611+-----------------------+-------------------------+---------------------------------+
612| Division | ``a // b`` | ``floordiv(a, b)`` |
613+-----------------------+-------------------------+---------------------------------+
614| Bitwise And | ``a & b`` | ``and_(a, b)`` |
615+-----------------------+-------------------------+---------------------------------+
616| Bitwise Exclusive Or | ``a ^ b`` | ``xor(a, b)`` |
617+-----------------------+-------------------------+---------------------------------+
618| Bitwise Inversion | ``~ a`` | ``invert(a)`` |
619+-----------------------+-------------------------+---------------------------------+
620| Bitwise Or | ``a | b`` | ``or_(a, b)`` |
621+-----------------------+-------------------------+---------------------------------+
622| Exponentiation | ``a ** b`` | ``pow(a, b)`` |
623+-----------------------+-------------------------+---------------------------------+
624| Identity | ``a is b`` | ``is_(a, b)`` |
625+-----------------------+-------------------------+---------------------------------+
626| Identity | ``a is not b`` | ``is_not(a, b)`` |
627+-----------------------+-------------------------+---------------------------------+
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000628| Indexed Assignment | ``obj[k] = v`` | ``setitem(obj, k, v)`` |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000629+-----------------------+-------------------------+---------------------------------+
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000630| Indexed Deletion | ``del obj[k]`` | ``delitem(obj, k)`` |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000631+-----------------------+-------------------------+---------------------------------+
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000632| Indexing | ``obj[k]`` | ``getitem(obj, k)`` |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000633+-----------------------+-------------------------+---------------------------------+
634| Left Shift | ``a << b`` | ``lshift(a, b)`` |
635+-----------------------+-------------------------+---------------------------------+
636| Modulo | ``a % b`` | ``mod(a, b)`` |
637+-----------------------+-------------------------+---------------------------------+
638| Multiplication | ``a * b`` | ``mul(a, b)`` |
639+-----------------------+-------------------------+---------------------------------+
640| Negation (Arithmetic) | ``- a`` | ``neg(a)`` |
641+-----------------------+-------------------------+---------------------------------+
642| Negation (Logical) | ``not a`` | ``not_(a)`` |
643+-----------------------+-------------------------+---------------------------------+
644| Right Shift | ``a >> b`` | ``rshift(a, b)`` |
645+-----------------------+-------------------------+---------------------------------+
Benjamin Peterson90f36732008-07-12 20:16:19 +0000646| Sequence Repetition | ``seq * i`` | ``repeat(seq, i)`` |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000647+-----------------------+-------------------------+---------------------------------+
648| Slice Assignment | ``seq[i:j] = values`` | ``setslice(seq, i, j, values)`` |
649+-----------------------+-------------------------+---------------------------------+
650| Slice Deletion | ``del seq[i:j]`` | ``delslice(seq, i, j)`` |
651+-----------------------+-------------------------+---------------------------------+
652| Slicing | ``seq[i:j]`` | ``getslice(seq, i, j)`` |
653+-----------------------+-------------------------+---------------------------------+
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000654| String Formatting | ``s % obj`` | ``mod(s, obj)`` |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000655+-----------------------+-------------------------+---------------------------------+
656| Subtraction | ``a - b`` | ``sub(a, b)`` |
657+-----------------------+-------------------------+---------------------------------+
Mark Summerfieldddca9f02007-09-13 14:54:30 +0000658| Truth Test | ``obj`` | ``truth(obj)`` |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000659+-----------------------+-------------------------+---------------------------------+
660| Ordering | ``a < b`` | ``lt(a, b)`` |
661+-----------------------+-------------------------+---------------------------------+
662| Ordering | ``a <= b`` | ``le(a, b)`` |
663+-----------------------+-------------------------+---------------------------------+
664| Equality | ``a == b`` | ``eq(a, b)`` |
665+-----------------------+-------------------------+---------------------------------+
666| Difference | ``a != b`` | ``ne(a, b)`` |
667+-----------------------+-------------------------+---------------------------------+
668| Ordering | ``a >= b`` | ``ge(a, b)`` |
669+-----------------------+-------------------------+---------------------------------+
670| Ordering | ``a > b`` | ``gt(a, b)`` |
671+-----------------------+-------------------------+---------------------------------+
672