__mul__() method is called to implement the arithmetic multiplication operation
*. For example to evaluate the expression
x * y, Python attempts to call
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.
In the following example, you create a custom class
Data and overwrite the
__mul__() method so that creates a new
Data object with the value being the product of the values of the two operands
b of type
class Data: def __init__(self, value): self.value = value def __mul__(self, other): return Data(self.value * other.value) a = Data(21) b = Data(2) c = a * b print(c.value) # 42
You have defined the dunder method so that the resulting product of two
Data objects is a
Data object itself:
print(type(c)) # <class '__main__.Data'>
If you hadn’t defined the
__mul__() method, Python would’ve raised a
How to Resolve TypeError: unsupported operand type(s) for *
Consider the following code snippet where you try to multiply two custom objects without defining the dunder method
class Data: def __init__(self, value): self.value = value a = Data(21) b = Data(2) c = a * b print(c.value)
Running this leads to the following error message on my computer:
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 9, in <module> c = a * b TypeError: unsupported operand type(s) for *: 'Data' and 'Data'
The reason for this error is that the
__mul__() dunder method has never been defined—and it is not defined for a custom object by default. So, to resolve the
TypeError: unsupported operand type(s) for *, you need to provide the
__mul__(self, other) method in your class definition as shown previously:
class Data: def __init__(self, value): self.value = value def __mul__(self, other): return Data(self.value * other.value)
Python __mul__ vs __rmul__
Say, you want to multiply two objects
print(x * y)
Python first tries to call the left object’s
x.__mul__(y). But this may fail for two reasons:
- The method
x.__mul__()is not implemented in the first place, or
- The method
x.__mul__()is implemented but returns a NotImplemented value indicating that the data types are incompatible.
If this fails, Python tries to fix it by calling the
y.__rmul__() for reverse multiplication on the right operator
y. If this method is implemented, Python knows that it doesn’t run into a potential problem of a non-commutative operation. If it would just execute
y.__mul__(x) instead of
x.__mul__(y), it could cause an error if the multiplication is non-commutative. That’s why
y.__rmul__(x) is needed which indicates that multiplication is possible after all.
So, the difference between
x.__rmul__(y) is that the former calculates
x * y whereas the latter calculates
y * x — both calling the respective multiplication method defined on object
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become 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.
Join my free webinar “How to Build Your High-Income Skill Python” and watch 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. 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.