__rmatmul__() method implements the reverse matrix multiplication
@ operation with reflected, swapped operands. So, when you call
x @ y, Python attempts to call
x.__matmul__(y). If the method is not implemented, Python attempts to call
__rmatmul__ on the right operand and if this isn’t implemented either, it 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.
Background Matrix Multiplication
In the following example, you create a custom class
Data and overwrite the
__matmul__() method that simply returns a dummy string. The real computation could be much more sophisticated, of course.
class Data: def __matmul__(self, other): return '... my result of matmul...' a = Data() b = Data() c = a @ b print(c) # ... my result of matmul...
@ operator was introduced to Python’s core syntax from 3.5 onwards thanks to PEP 465. Its only goal is to solve the problem of matrix multiplication. It even comes with a nice mnemonic –
@ is * for mATrices.
It is unusual that
@ was added to the core Python language when it’s only used with certain libraries. Fortunately, the only other time we use
@ is for decorator functions. So you are unlikely to get confused.
Python __matmul__ vs __rmatmul__
Say, you want to calculate the
@ operation on two custom objects
print(x @ y)
Python first tries to call the left object’s
x.__matmul__(y). But this may fail for two reasons:
- The method
x.__matmul__()is not implemented in the first place, or
- The method
x.__matmul__()is implemented but returns a
NotImplementedvalue indicating that the data types are incompatible.
If this fails, Python tries to fix it by calling the
y.__rmatmul__() for reverse matrix multiplication on the right operand
If the reverse matrix multiplication 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.__matmul__(x) instead of
x.__matmul__(y), the result would be wrong because the operation may be non-commutative when defined as a custom operation. That’s why
y.__rmatmul__(x) is needed.
So, the difference between
x.__rmatmul__(y) is that the former calculates
x @ y whereas the latter calculates
y @ x — both calling the respective method defined on the object
A nice little trick is to indirectly define matrix multiplication on a data type that doesn’t support it and that cannot be altered — such as a basic data type like a list — by implementing
__rmatmul__ on the other custom class over which one may have control.
You can see this in effect here where we attempt to call the operation on the left operand
x—but as it’s not implemented, Python simply calls the reverse operation on the right operand
class Data_1: pass class Data_2: def __rmatmul__(self, other): return 'called reverse matmul' x = Data_1() y = Data_2() print(x @ y) # called reverse matmul
To understand this operation in detail, feel free to read over our tutorial or watch the following video:
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.