Python __rdivmod__() Magic Method


object.__rdivmod__(self, other)

The Python __rdivmod__() method implements the divmod() built-in function with reflected, swapped operands. So, when you call divmod(x, y), Python attempts to call x.__divmod__(y). If the method is not implemented, Python attempts to call __rdivmod__ on the right operand. Only if this isn’t implemented either, it raises a TypeError.

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 Divmod

Python divmod() visual explanation

Python’s built-in divmod(a, b) function takes two integer or float numbers a and b as input arguments and returns a tuple (a // b, a % b).

In case of float inputs, divmod() still returns the division without remainder by rounding down to the next round number.

To understand this operation in detail, feel free to read over our tutorial or watch the following video:

What’s the Difference Between __divmod__() and __rdivmod__()?

Say, you want to apply the divmod() function to two objects x and y:

print(divmod(x, y))

Python first tries to call the left object’s __divmod__() method x.__divmod__(y). But this may fail for two reasons:

  1. The method x.__divmod__() is not implemented in the first place, or
  2. The method x.__divmod__() 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.__rdivmod__() for reverse divmod 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.__divmod__(x) instead of x.__divmod__(y), it could cause an error if the operation is non-commutative. That’s why y.__rdivmod__(x) is needed.

The difference between x.__divmod__(y) and x.__rdivmod__(y) is that the former calculates divmod(x, y) whereas the latter calculates divmod(y, x) — both calling it on object x.

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 y.

class Data_1:

class Data_2:
    def __rdivmod__(self, other):
        return 'called reverse divmod'

x = Data_1()
y = Data_2()

print(divmod(x, y))
# called reverse divmod


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.

Join the free webinar now!