The `operator` module contains functions that perform the same operations as man of the built-in operators.

Module: operator
Purpose: Functional interface to built-in operators.
Python Version: 1.4 and later

Description:

Functional programming using iterators occasionally requires you to create small functions for simple expressions. Sometimes these can be expressed as `lambda` functions. But for some operations, you don’t need to define your own function at all. The operator module defines functions that correspond to built-in operations for arithmetic, and comparison as well as sequence and dictionary operations.

Logical Operations:

There are logical operations for determining the boolean equivalent for a value, negating that to create the opposite boolean value, and comparing objects to see if they are identical.

`from operator import *a = -1b = 5print 'a =', aprint 'b =', bprint 'not_(a):', not_(a)print 'truth(a):', truth(a)print 'is_(a, b):', is_(a,b)print 'is_not(a, b):', is_not(a,b)`

`\$ python operator_boolean.pya = -1b = 5not_(a): Falsetruth(a): Trueis_(a, b): Falseis_not(a, b): True`

Comparison Operators:

All of the rich comparison operators are supported:

`from operator import *a = 1b = 5.0print 'a =', aprint 'b =', bfor func in (lt, le, eq, ne, ge, gt): print '%s(a, b):' % func.__name__, func(a, b)`

`\$ python operator_comparisons.pya = 1b = 5.0lt(a, b): Truele(a, b): Trueeq(a, b): Falsene(a, b): Truege(a, b): Falsegt(a, b): False`

Arithmetic Operators:

The arithmetic operators for manipulating numerical values are also supported.

`from operator import *a = -1b = 5.0c = 2d = 6print 'a =', aprint 'b =', bprint 'c =', cprint 'd =', dprint '\nPositive/Negative:'print 'abs(a):', abs(a)print 'neg(a):', neg(a)print 'neg(b):', neg(b)print 'pos(a):', pos(a)print 'pos(b):', pos(b)print '\nArithmetic:'print 'add(a, b):', add(a, b)print 'div(a, b):', div(a, b)print 'div(d, c):', div(d, c)print 'floordiv(a, b):', floordiv(a, b)print 'floordiv(d, c):', floordiv(d, c)print 'mod(a, b):', mod(a, b)print 'mul(a, b):', mul(a, b)print 'pow(c, d):', pow(c, d)print 'sub(b, a):', sub(b, a)print 'truediv(a, b):', truediv(a, b)print 'truediv(d, c):', truediv(d, c)print '\nBitwise:'print 'and_(c, d):', and_(c, d)print 'invert(c):', invert(c)print 'lshift(c, d):', lshift(c, d)print 'or_(c, d):', or_(c, d)print 'rshift(d, c):', rshift(d, c)print 'xor(c, d):', xor(c, d)`

Notice the two division operators: `floordiv` (pre-3.0 integer division) and `truediv` (floating point division).

`\$ python operator_math.pya = -1b = 5.0c = 2d = 6Positive/Negative:abs(a): 1neg(a): 1neg(b): -5.0pos(a): -1pos(b): 5.0Arithmetic:add(a, b): 4.0div(a, b): -0.2div(d, c): 3floordiv(a, b): -1.0floordiv(d, c): 3mod(a, b): 4.0mul(a, b): -5.0pow(c, d): 64sub(b, a): 6.0truediv(a, b): -0.2truediv(d, c): 3.0Bitwise:and_(c, d): 2invert(c): -3lshift(c, d): 128or_(c, d): 6rshift(d, c): 1xor(c, d): 4`

Sequence Operators:

The operators for working with sequences can be divided into roughly 4 groups for building up sequences, searching, working with items, and removing items from sequences.

`from operator import *a = [ 1, 2, 3 ]b = [ 'a', 'b', 'c' ]print 'a =', aprint 'b =', bprint '\nConstructive:'print 'concat(a, b):', concat(a, b)print 'repeat(a, 3):', repeat(a, 3)print '\nSearching:'print 'contains(a, 1):', contains(a, 1)print 'contains(b, "d"):', contains(b, "d")print 'countOf(a, 1):', countOf(a, 1)print 'countOf(b, "d"):', countOf(b, "d")print 'indexOf(a, 5):', indexOf(a, 1)print '\nAccess Items:'print 'getitem(b, 1):', getitem(b, 1)print 'getslice(a, 1, 3)', getslice(a, 1, 3)print 'setitem(b, 1, "d"):', setitem(b, 1, "d"), ',after b =', bprint 'setslice(a, 1, 3, [4, 5]):', setslice(a, 1, 3, [4, 5]), ', after a =', aprint '\nDestructive:'print 'delitem(b, 1):', delitem(b, 1), ',after b =', bprint 'delslice(a, 1, 3):', delslice(a, 1, 3), ', after a =', a`

`\$ python operator_sequences.pya = [1, 2, 3]b = ['a', 'b', 'c']Constructive:concat(a, b): [1, 2, 3, 'a', 'b', 'c']repeat(a, 3): [1, 2, 3, 1, 2, 3, 1, 2, 3]Searching:contains(a, 1): Truecontains(b, "d"): FalsecountOf(a, 1): 1countOf(b, "d"): 0indexOf(a, 5): 0Access Items:getitem(b, 1): bgetslice(a, 1, 3) [2, 3]setitem(b, 1, "d"): None ,after b = ['a', 'd', 'c']setslice(a, 1, 3, [4, 5]): None , after a = [1, 4, 5]Destructive:delitem(b, 1): None ,after b = ['a', 'c']delslice(a, 1, 3): None , after a = [1]`

In-place Operators:

In addition to the standard operators, many types of objects support “in-place” modification through special operators such as `+=`. There are equivalent functions for in-place modifications, too:

`from operator import *a = -1b = 5.0c = [ 1, 2, 3 ]d = [ 'a', 'b', 'c']print 'a =', aprint 'b =', bprint 'c =', cprint 'd =', dprint 'iadd(a, b):', iadd(a, b)a = iadd(a, b)print 'a = iadd(a, b) =>', aprint 'iconcat(c, d):', iconcat(c, d)c = iconcat(c, d)print 'c = iconcat(c, d) =>', c`

These examples only demonstrate a couple of the functions. Refer to the stdlib documentation for complete details.

`\$ python operator_inplace.pya = -1b = 5.0c = [1, 2, 3]d = ['a', 'b', 'c']iadd(a, b): 4.0a = iadd(a, b) => 4.0iconcat(c, d): [1, 2, 3, 'a', 'b', 'c']c = iconcat(c, d) => [1, 2, 3, 'a', 'b', 'c', 'a', 'b', 'c']`

Attribute and Item “Getters”:

One of the most unusual features of the `operator` module is the notion of “getters”. These are callable objects constructed at runtime to retrieve attributes of items from objects or sequences. Getters are especially useful when working with iterators or generator sequences, where they are intended to incur less overhead than a lambda or Python function.

Attribute getters work like `lambda x, n='attrname': getattr(x, n)`:

`from operator import *class MyObj(object): """example class for attrgetter""" def __init__(self, arg): super(MyObj, self).__init__() self.arg = arg def __repr__(self): return 'MyObj(%s)' % self.argl = [ MyObj(i) for i in xrange(5) ]print lg = attrgetter('arg')vals = [ g(i) for i in l ]print vals`

`\$ python operator_attrgetter.py[MyObj(0), MyObj(1), MyObj(2), MyObj(3), MyObj(4)][0, 1, 2, 3, 4]`

While item getters work like `lambda x, y=5: x[y]`:

`from operator import *print 'Dictionaries:'l = [ dict(val=i) for i in xrange(5) ]print lg = itemgetter('val')vals = [ g(i) for i in l ]print valsprint 'Tuples:'l = [ (i, i*2) for i in xrange(5) ]print lg = itemgetter(1)vals = [ g(i) for i in l ]print vals`

Item getters work with mappings as well as sequences.

`\$ python operator_itemgetter.pyDictionaries:[{'val': 0}, {'val': 1}, {'val': 2}, {'val': 3}, {'val': 4}][0, 1, 2, 3, 4]Tuples:[(0, 0), (1, 2), (2, 4), (3, 6), (4, 8)][0, 2, 4, 6, 8]`

The functions in the `operator` module work via the standard Python interfaces for their operations, so they work with your classes as well as the builtin types.

`from operator import *class MyObj(object): """Example for operator overloading""" def __init__(self, val): super(MyObj, self).__init__() self.val = val return def __str__(self): return 'MyObj(%s)' % self.val def __lt__(self, other): """compare for less-than""" print 'Testing %s < %s' % (self, other) return self.val < other.val def __add__(self, other): """add values""" print 'Adding %s + %s' % (self, other) return MyObj(self.val + other.val)a = MyObj(1)b = MyObj(2)print lt(a, b)print add(a, b)`

`\$ python operator_classes.pyTesting MyObj(1) < MyObj(2)TrueAdding MyObj(1) + MyObj(2)MyObj(3)`

Type Checking:

Besides the actual operators, there are functions for testing API compliance for mapping, number, and sequence types. The tests are not perfect, since the interfaces are not strictly defined, but they do give you some idea of what is supported.

`from operator import *class NoType(object): """Supports none of the type APIs""" class MultiType(object): """Supports multiple type APIs""" def __len__(self): return 0 def __getitem__(self, name): return 'mapping' def __int__(self): return 0o = NoType()t = MultiType()for func in (isMappingType, isNumberType, isSequenceType): print '%s(o):' % func.__name__, func(o) print '%s(t):' % func.__name__, func(t)`

`\$ python operator_typechecking.pyisMappingType(o): FalseisMappingType(t): TrueisNumberType(o): FalseisNumberType(t): TrueisSequenceType(o): FalseisSequenceType(t): True`

References:

ASPN: Python Cookbook: using the operator module w/map (plus a bit on itertools and generator exp)
Python Module of the Week Home