Python Glossary Русский

all Python 3operatorfunction

operator.add

arithmetic

Return a + b (the functional form of `+`).

Syntax

operator.add(a, b)

Use it wherever a function is expected rather than an expression: functools.reduce(operator.add, xs), map over two sequences, a "symbol → action" table in a calculator. It is faster than a lambda and, unlike one, can be pickled — which matters for multiprocessing. To total numbers prefer sum(); to join strings prefer str.join, since reduce with add concatenates in quadratic time.

Examples

import operator
import functools
print(operator.add(1, 2))   # → 3
print(operator.add(1, 2) == 1 + 2)   # → True
print(operator.add('py', 'thon'))   # → python
print(functools.reduce(operator.add, [1, 2, 3, 4]))   # → 10
print(list(map(operator.add, [1, 2, 3], [10, 20, 30])))   # → [11, 22, 33]

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy