Python provides the operator
x %= y to calculate the modulo operation
x % y, and assign the result in-place to the first operands variable
x. You can set up the in-place modulo behavior for your own class by overriding the magic “dunder” method
__imod__(self, other) in your class definition.
>>> x = 9 >>> x %= 4 >>> x 1
x %= y is syntactical sugar for the longer-form
x = x % y:
>>> x = 9 >>> x = x % 4 >>> x 1
Let’s explore some examples on different data types of the operands.
%= operator on integer operands stores the remainder of the division of both operands in the left-hand operands’ variable name.
>>> x = 42 >>> x %= 40 >>> x 2
If at least one of the operands is a float value, the result is also a float—float is infectious!
>>> x = 42 >>> x %= 40.0 >>> x 2.0
Incompatible Data Type
What if two operands have an incompatible data type—unlike floats and integers? For example, if you try to calculate in-place modulo of two list variables?
>>> [1, 2] % [3, 4] Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> [1, 2] % [3, 4] TypeError: unsupported operand type(s) for %: 'list' and 'list'
The result of incompatible operation is a
TypeError. You can fix it by using only compatible data types for the in-place modulo operation.
Can you use in-place modulo on custom objects? Yes!
Python In-Place Modulo Magic Method
To use the in-place modulo operator
%= on custom objects, you need to define the
__imod__() method (“dunder method”, “magic method”) that takes two arguments
other, updates the first argument
self with the remainder of the division, and returns the updated object.
In the following code, you use the in-place modulo on two
Data objects by defining a custom
class Data: def __init__(self, data): self.data = data def __imod__(self, other): self.data %= other.data return self x = Data(40) y = Data(11) x %= y print(x.data) # 7
You can see that the content of the first operand is updated as a result of the in-place modulo operation.
Modulo Video Explanation [Background]
Related Article: Python Modulo Operator
Python In-Place Operators
In-place assignment operators (also called compound assignment operators) perform an operation in-place on a variable provided as first operand. They overwrite the value of the first operand variable with the result of the operation when performing the operator without assignment. For example,
x += 3 is the same as
x = x + 3 of first calculating the result of
x +3 and then assigning it to the variable x.
|Operator||Name||Short Example||Equivalent Long Example|
|In-place Integer Division|
|In-place Bitwise And|
|In-place Bitwise Or|
|In-place Bitwise XOR|
|In-place Bitwise Shift Right|
|<<=||In-place Bitwise Shift Left|
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.