In what situation should the built-in 'operator' module be used in python?

Possibly the most popular usage is operator.itemgetter. Given a list lst of tuples, you can sort by the ith element by: lst.sort(key=operator.itemgetter(i))

Certainly, you could do the same thing without operator by defining your own key function, but the operator module makes it slightly neater.

As to the rest, python allows a functional style of programming, and so it can come up -- for instance, Greg's reduce example.

You might argue: "Why do I need operator.add when I can just do: add = lambda x, y: x+y?" The answers are:

  1. operator.add is (I think) slightly faster.
  2. It makes the code easier to understand for you, or another person later, looking at it. They don't need to look for the definition of add, because they know what the operator module does.
  3. operator.add is picklable, while lambda is not. This means that the function can be saved to disk or passed between processes.

One example is in the use of the reduce() function:

>>> import operator
>>> a = [2, 3, 4, 5]
>>> reduce(lambda x, y: x + y, a)
14
>>> reduce(operator.add, a)
14

The module is useful when you need to pass a function as an argument to something. There are then two options: use the operator module, or define a new function (using def or lambda). If you define a function on the fly, this can create a problem if you need to pickle this function, either to save it to disk or to pass it between processes. While itemgetter is picklable, dynamically defined functions (either with def or lambda) are not. In the following example, replacing itemgetter with a lambda expression will result in a PicklingError.

from operator import itemgetter

def sort_by_key(sequence, key):
    return sorted(sequence, key=key)

if __name__ == "__main__":
    from multiprocessing import Pool

    items = [([(1,2),(4,1)], itemgetter(1)),
             ([(5,3),(2,7)], itemgetter(0))]

    with Pool(5) as p:
        result = p.starmap(sort_by_key, items)
    print(result)