Python Glossary Русский

all Python 3operatorfunction

operator.imul

in-place

In-place multiplication: a *= b; for lists, in-place repetition.

Syntax

operator.imul(a, b)

For a list it repeats the contents in place, and all copies point to the same nested objects: after imul([[]], 3) appending to the first inner list shows up in all three. For numbers and strings assign the result — they are immutable. A list of lists is safer to build with a comprehension: [[] for _ in range(3)].

Examples

import operator
print(operator.imul(3, 4))   # → 12
print(operator.imul('ab', 3))   # → ababab
nums = [1, 2]
print(operator.imul(nums, 2) is nums)   # → True
print(nums)   # → [1, 2, 1, 2]
n = 3   # int неизменяем: результат надо присвоить обратно
print(operator.imul(n, 4), n)   # → 12 3

See also

Open in the full glossarydocs.python.orgReport an inaccuracy