Python __rdiv__ Magic Method

Rate this post

The Python __rdiv__() magic method overrides the reverse division operation for a custom object in Python 2. In Python 3, it was replaced by the __rtruediv__() and __rfloordiv__() dunder methods.

  • The Python __rtruediv__() method is called to implement the normal division operation / called true division and apply it in reverse.
  • The Python __rfloordiv__() method implements the reverse integer division operation.

Syntax

object.__rdiv__(self, other)

The __rdiv__() method implements the reverse true division operation in Python 2 with reflected, swapped operands. So, when you call x / y, Python attempts to call x.__div__(y). If the method is not implemented, Python attempts to call __rdiv__ on the right operand and 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.

Example

To override the reverse division operator in Python 2, define the __rdiv__ method in the class. Python will then call it on the second operand as a backup if the __div__ method is not defined for the first operand.

# Works for Python 2 Only:
class Data:
    def __rdiv__(self, other):
        return '... my result of rdiv...'


a = Data()
b = Data()
c = a / b

print(c)
# ... my result of rdiv...

If you want the same example in Python 3, read on!

Background Reverse True Division Python 3

The Python __rtruediv__() method is called to implement the normal division operation / called true division—as opposed to the floor division operation //.

For example to evaluate the expression x / y, Python attempts to call x.__truediv__(y).

In the following example, you create a custom class Data and overwrite the __truediv__() method so that it returns a dummy string when trying to divide two Data objects using the true division operation a / b.

class Data:
        
    def __rtruediv__(self, other):
        return '... my result of rtruediv...'


a = Data()
b = Data()
c = a / b

print(c)
# ... my result of rtruediv...

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

References:

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.

Join the free webinar now!