Operators as alternative to an infix operator
suggest changeFor every infix operator, for example +, there is an operator function (operator.add for +):
1 + 1
# Output: 2
from operator import add
add(1, 1)
# Output: 2
Even though the documentation says these arithmetic functions are for numbers, other types work when the matching operator does:
from operator import mul
mul('a', 10)
# Output: 'aaaaaaaaaa'
mul([3], 3)
# Output: [3, 3, 3]
See also: mapping from operation to operator function in the official Python documentation.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents