4

What is the point of python's operator module? There are many obviously redundant functions there and I don't understand why should one prefer to use these functions rather than other ways to do the same thing.

For example:

>>> import operator
>>> operator.truth(0)
False
>>> bool(0)
False

seem to do exactly the same thing.

wim
  • 338,267
  • 99
  • 616
  • 750

3 Answers3

8

Its sometimes useful to be able to access the functionality of an operator but as a function. For example to add two numbers together you could do.

>> print(1 + 2)
3

You could also do

>> import operator
>> print(operator.add(1, 2))
3

A use case for the function approach could be you need to write a calculator function which returns an answer given a simple formula.

import operator as _operator

operator_mapping = {
    '+': _operator.add,
    '-': _operator.sub,
    '*': _operator.mul,
    '/': _operator.truediv,
}

def calculate(formula):
    x, operator, y = formula.split(' ')

    # Convert x and y to floats so we can perform mathematical
    # operations on them.
    x, y = map(float, (x, y))

    return operator_mapping[operator](x, y)

print(calculate('1 + 2'))  # prints 3.0
Mat
  • 1,345
  • 9
  • 15
3

For completeness and consistency. Because having all operators in one place lets you do dynamic lookups later on:

getattr(operator, opname)(*arguments)

Omitting some operations because they are redundant would defeat that purpose. And because Python names are just references, it is cheap and easy to add a name to the operator module that is simply another reference.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Not very convincing answer for me, because different operations take different numbers of arguments. – wim Nov 06 '16 at 16:27
  • 1
    @wim: I did pass in arguments dynamically here. You can trivially group string operators into groups by the number of operators they take too. – Martijn Pieters Nov 06 '16 at 16:28
  • Don't really understand the use-case and seems to violate "one obvious way to do it". Are you aware of any project on github where the code actually used dynamic lookups on operator module like this? – wim Nov 06 '16 at 16:31
  • 1
    @wim: we could slog through [these results](https://searchcode.com/?q=getattr%28operator&loc=0&loc2=10000&lan=19). I can't narrow it down to an exact match, but I see `pandas` uses `getattr(operator, ...)` in their tests. – Martijn Pieters Nov 06 '16 at 16:42
  • @MartijnPieters What do you mean by doing dynamic lookups? Can you write an example? –  Nov 06 '16 at 17:07
  • @student: I already did in the answer; given `arguments = [42]`, setting `opname = 'truth'` will give you a different result from `opname = 'neg'`, which is different again from `opname = 'index'`. – Martijn Pieters Nov 06 '16 at 17:09
1

Given the existence of bool, it's hard to think of any use-case for operator.truth these days. Note that bool was new in 2.2.1, and operator predates that, so it may only exist now for historical reasons. There are also other useless functions in the operator module, such as operator.abs, which simply calls the built-in abs.

Not everything in operator is entirely useless, though - operator's C implementation, if available, can offer performance gains over pure Python implementations. The itemgetter, attrgetter and methodcaller functions are more readable and generally better performing utility functions for tasks which are often handled by lambda functions.

wim
  • 338,267
  • 99
  • 616
  • 750