sphinx / Doc-3k / library / operator.rst

:mod:`operator` --- Standard operators as functions.

The :mod:`operator` module exports a set of functions implemented in C corresponding to the intrinsic operators of Python. For example, operator.add(x, y) is equivalent to the expression x+y. The function names are those used for special class methods; variants without leading and trailing __ are also provided for convenience.

The functions fall into categories that perform object comparisons, logical operations, mathematical operations, sequence operations, and abstract type tests.

The object comparison functions are useful for all objects, and are named after the rich comparison operators they support:

The logical operations are also generally applicable to all objects, and support truth tests, identity tests, and boolean operations:

The mathematical and bitwise operations are the most numerous:

Operations which work with sequences include:

Many operations have an "in-place" version. The following functions provide a more primitive access to in-place operators than the usual syntax does; for example, the statement x += y is equivalent to x = operator.iadd(x, y). Another way to put it is to say that z = operator.iadd(x, y) is equivalent to the compound statement z = x; z += y.

The :mod:`operator` module also defines a few predicates to test the type of objects.


Be careful not to misinterpret the results of these functions; only :func:`isCallable` has any measure of reliability with instance objects. For example:

>>> class C:
...     pass
>>> import operator
>>> o = C()
>>> operator.isMappingType(o)

Example: Build a dictionary that maps the ordinals from 0 to 255 to their character equivalents.

>>> import operator
>>> d = {}
>>> keys = range(256)
>>> vals = map(chr, keys)
>>> map(operator.setitem, [d]*len(keys), keys, vals)

The :mod:`operator` module also defines tools for generalized attribute and item lookups. These are useful for making fast field extractors as arguments for :func:`map`, :func:`sorted`, :meth:`itertools.groupby`, or other functions that expect a function argument.


>>> from operator import itemgetter
>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
>>> getcount = itemgetter(1)
>>> map(getcount, inventory)
[3, 2, 5, 1]
>>> sorted(inventory, key=getcount)
[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]

Mapping Operators to Functions

This table shows how abstract operations correspond to operator symbols in the Python syntax and the functions in the :mod:`operator` module.

Operation Syntax Function
Addition a + b add(a, b)
Concatenation seq1 + seq2 concat(seq1, seq2)
Containment Test o in seq contains(seq, o)
Division a / b div(a, b) # without __future__.division
Division a / b truediv(a, b) # with __future__.division
Division a // b floordiv(a, b)
Bitwise And a & b and_(a, b)
Bitwise Exclusive Or a ^ b xor(a, b)
Bitwise Inversion ~ a invert(a)
Bitwise Or a | b or_(a, b)
Exponentiation a ** b pow(a, b)
Identity a is b is_(a, b)
Identity a is not b is_not(a, b)
Indexed Assignment o[k] = v setitem(o, k, v)
Indexed Deletion del o[k] delitem(o, k)
Indexing o[k] getitem(o, k)
Left Shift a << b lshift(a, b)
Modulo a % b mod(a, b)
Multiplication a * b mul(a, b)
Negation (Arithmetic) - a neg(a)
Negation (Logical) not a not_(a)
Right Shift a >> b rshift(a, b)
Sequence Repitition seq * i repeat(seq, i)
Slice Assignment seq[i:j] = values setslice(seq, i, j, values)
Slice Deletion del seq[i:j] delslice(seq, i, j)
Slicing seq[i:j] getslice(seq, i, j)
String Formatting s % o mod(s, o)
Subtraction a - b sub(a, b)
Truth Test o truth(o)
Ordering a < b lt(a, b)
Ordering a <= b le(a, b)
Equality a == b eq(a, b)
Difference a != b ne(a, b)
Ordering a >= b ge(a, b)
Ordering a > b gt(a, b)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.