Python's operator module provides functions that perform inplace operations - combining a computation and an assignment into a single function call. These functions mirror Python's augmented assignment operators (+=, -=, *=, etc.) and are especially useful when you need to pass an operator as an argument to higher-order functions like map(), reduce(), or custom callbacks.
In this guide, you will learn what inplace operators are, how each function works with practical examples, and when to use the operator module functions instead of standard operators.
What Are Inplace Operators?
An inplace operator performs an operation and assigns the result back in one step. For example, x += 5 adds 5 to x and stores the result in x. The operator module provides equivalent functions:
import operator
x += y
x = operator.iadd(x, y)The key difference is that operator.iadd() is a callable function - you can store it in a variable, pass it to other functions, or use it in dispatch tables.
iadd() - Inplace Addition (+=)
Adds two values and returns the result. Equivalent to a += b:
import operator
result = operator.iadd(2, 3)
print(f"2 + 3 = {result}")Output:
2 + 3 = 5With mutable types like lists, iadd() extends the list in place:
import operator
numbers = [1, 2, 3]
operator.iadd(numbers, [4, 5])
print(numbers)Output:
[1, 2, 3, 4, 5]isub() - Inplace Subtraction (-=)
Subtracts the second value from the first and returns the result. Equivalent to a -= b:
import operator
result = operator.isub(2, 3)
print(f"2 - 3 = {result}")Output:
2 - 3 = -1imul() - Inplace Multiplication (*=)
Multiplies two values and returns the result. Equivalent to a *= b:
import operator
result = operator.imul(2, 3)
print(f"2 * 3 = {result}")Output:
2 * 3 = 6With sequences, imul() repeats the sequence:
import operator
word = "ab"
result = operator.imul(word, 3)
print(result)Output:
abababitruediv() - Inplace True Division (/=)
Divides the first value by the second and returns a float. Equivalent to a /= b:
import operator
result = operator.itruediv(10, 5)
print(f"10 / 5 = {result}")Output:
10 / 5 = 2.0Note that the result is always a float, even when the division is exact.
imod() - Inplace Modulus (%=)
Returns the remainder of dividing the first value by the second. Equivalent to a %= b:
import operator
result = operator.imod(10, 6)
print(f"10 % 6 = {result}")Output:
10 % 6 = 4iconcat() - Inplace Concatenation (+= for Sequences)
Concatenates two sequences and returns the result. This works with strings, lists, tuples, and other sequence types:
import operator
first = "tutorial"
second = "reference"
result = operator.iconcat(first, second)
print(f"Concatenated string: {result}")Output:
Concatenated string: tutorialreferenceWith lists, iconcat() modifies the first list in place (since lists are mutable):
import operator
list_a = [1, 2, 3]
list_b = [4, 5, 6]
operator.iconcat(list_a, list_b)
print(list_a)Output:
[1, 2, 3, 4, 5, 6]Complete Reference Table
| Function | Equivalent Operator | Operation | Example | Result |
|---|---|---|---|---|
operator.iadd(a, b) |
a += b |
Addition | iadd(2, 3) |
5 |
operator.isub(a, b) |
a -= b |
Subtraction | isub(2, 3) |
-1 |
operator.imul(a, b) |
a *= b |
Multiplication | imul(2, 3) |
6 |
operator.itruediv(a, b) |
a /= b |
True division | itruediv(10, 5) |
2.0 |
operator.imod(a, b) |
a %= b |
Modulus | imod(10, 6) |
4 |
operator.iconcat(a, b) |
a += b (sequences) |
Concatenation | iconcat("ab", "cd") |
"abcd" |
Practical Example: Using Inplace Operators as Callbacks
The primary advantage of the operator module functions is that they are first-class callable objects. This makes them useful in functional programming patterns:
import operator
from functools import reduce
numbers = [10, 20, 30, 40, 50]
total = reduce(operator.iadd, numbers)
print(f"Sum of {numbers} = {total}")Output:
Sum of [10, 20, 30, 40, 50] = 150You cannot pass the += operator directly to reduce(), but operator.iadd makes it possible.
Building a Dynamic Calculator
import operator
operations = {
"add": operator.iadd,
"subtract": operator.isub,
"multiply": operator.imul,
"divide": operator.itruediv,
"modulus": operator.imod,
}
def calculate(op_name, a, b):
if op_name not in operations:
return f"Unknown operation: {op_name}"
return operations[op_name](a, b)
print(calculate("add", 15, 7))
print(calculate("multiply", 6, 8))
print(calculate("divide", 100, 3))Output:
22
48
33.333333333333336Common Mistake: Expecting Immutable Types to Change In Place
A frequent source of confusion is expecting inplace operators to modify immutable values:
import operator
x = 10
operator.iadd(x, 5)
print(x) # Still 10!Output:
10The function returns the new value, but since integers are immutable, x is not modified. You must capture the return value:
import operator
x = 10
x = operator.iadd(x, 5)
print(x) # 15Output:
15Conclusion
Python's inplace operator functions in the operator module (iadd(), isub(), imul(), itruediv(), imod(), and iconcat()) provide callable equivalents of augmented assignment operators.
They are most valuable when you need to pass an operator as a function argument to higher-order functions like reduce(), sorting keys, or dynamic dispatch tables.
Remember that for immutable types, these functions return new values without modifying the originals, so always capture the return value. For mutable types like lists, the operations modify the object in place, making them truly "inplace."