Operators as alternative to an infix operator

suggest change

For 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents