Python Glossary Русский

all Python 3operatorfunction

operator.iadd

in-place

In-place addition a += b: it mutates a mutable a and behaves like add for an immutable one.

Syntax

operator.iadd(a, b)

The return value has to be assigned: iadd(x, 1) on a number does not change x, because int is immutable — write x = iadd(x, 1). For a list iadd mutates it in place and accepts any iterable: iadd([1], (2,)) gives [1, 2], although [1] + (2,) raises TypeError. Other references to the list will see the change.

Examples

import operator
print(operator.iadd(1, 2))   # → 3
print(operator.iadd(1, 2) == 1 + 2)   # → True
nums = [1, 2]
print(operator.iadd(nums, [3]))   # → [1, 2, 3]
print(nums)   # → [1, 2, 3]
t = (1, 2)
print(operator.iadd(t, (3,)))   # → (1, 2, 3)
print(t)   # → (1, 2)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy