__imod__() magic method implements the in-place modulo operation
x %= y that calculates the modulo operation
x % y, and assigns the result to the first operands variable
x. This type of in-place operation is also called augmented arithmetic assignment. The method simply returns the new value to be assigned to the first operand.
- When you call
x %= y, Python first attempts to call
- If this is not implemented, it tries the normal modulo operation
- If this is not implemented either, it tries reverse modulo operation
y.__rmod__(x)with swapped operands.
The result is then assigned to the first operand
x. If none of those operations is implemented, Python raises a
We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.
Basic Example Overriding __imod__
In the following code example, you create a class
Data and define the magic method
- The “self” argument is the default argument of each method and it refers to the object on which it is called—in our case, the first operand of the in-place operation.
- The “other” argument of the in-place method refers to the second operand, i.e.,
yin the in-place operation
x %= y.
The return value of the operation returns a dummy string
'finxter 42' to be assigned to the first operand. In practice, this would be the result of the in-place modulo operation.
class Data: def __imod__(self, other): return 'finxter 42' x = Data() y = Data() x %= y print(x) # finxter 42
In-Place Modulo %= Without __imod__()
To support in-place modulo on a custom class, you don’t have to overwrite the in-place
__imod__() method. Because if the method is not defined, Python will fall back to the normal
__mod__() method and assign its result to the first operand.
Here’s an example:
class Data: def __mod__(self, other): return 'finxter 42' x = Data() y = Data() x %= y print(x) # finxter 42
Even though the
__imod__() method is not defined, the in-place modulo operation
x %= y still works due to the
__mod__() “fallback” magic method!
In-Place Modulo %= Without __imod__() and __mod__()
To support in-place modulo
x %= y on a custom class, you don’t even have to overwrite any of the
x.__mod__(y) methods. If both are not defined, Python falls back to the reverse
y.__rmod__(x) method and assigns its result to the first operand.
Here’s an example where you create a custom class for the first operand that doesn’t support the modulo operation. Then you define a custom class for the second operand that defines the
__rmod__() method. For the in-place operation, Python falls back to the
__rmod__() method defined on the second operand and assigns it to the first operand
class Data_1: pass class Data_2: def __rmod__(self, other): return 'finxter 42' x = Data_1() y = Data_2() x %= y print(x) # finxter 42
TypeError: unsupported operand type(s) for %=
If you try to perform in-place modulo
x %= y but neither
y.__rmod(x) is defined, Python raises a “
TypeError: unsupported operand type(s) for %=". To fix this error, simply define any of those methods before performing the in-place operation.
class Data: pass x = Data() y = Data() x %= y
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 8, in <module> x %= y TypeError: unsupported operand type(s) for %=: 'Data' and 'Data'
In Python like in many other programming languages, the modulo operator is represented by the percent
% symbol. It returns the remainder of dividing the left by the right operand.
For example, the modulo operation 7%2 returns 1 because seven divided by two is three with remainder 1.
The figure shows how the modulo operator works in Python for the example
- There’s a set with seven elements. This is the dividend of the operation.
- The divisor is 3. You divide the set of seven elements into packages of three elements.
- The remainder is 1. When dividing the set of seven elements into packages of three elements, one remains. This is the result of the modulo operation.
You can learn more about Python division and integer division in this detailed blog guides:
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
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 that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a 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.