Operators as alternative to an infix operator

Download python eBook

Example

For every infix operator, e.g. + there is a operator-function (operator.add for +):

1 + 1
# Output: 2
from operator import add
add(1, 1)
# Output: 2

even though the main documentation states that for the arithmetic operators only numerical input is allowed it is possible:

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.

Stats

Contributors: 1
2016-03-25
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook