__rfloordiv__() method implements the reverse floor (integer) division operation with reflected, swapped operands. So, when you call
x // y, Python attempts to call
x.__floordiv__(y). If the method is not implemented, Python attempts to call
__rfloordiv__ 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 Floor Division
__floordiv__() method is called to implement the integer division operation
// called floor division—as opposed to the true division operation
For example to evaluate the expression
x // y, Python attempts to call
In the following example, you create a custom class
Data and overwrite the
__floordiv__() method so that it returns a dummy string when trying to divide two
Data objects using the floor division operation
a // b.
class Data: def __floordiv__(self, other): return '... my result of floordiv...' a = Data() b = Data() c = a // b print(c) # ... my result of floordiv...
To understand this operation in detail, feel free to read over our tutorial or watch the following video:
What’s the Difference Between __floordiv__() and __rfloordiv__()?
Say, you want to divide two objects
y using floor division:
print(x // y)
Python first tries to call the left object’s
x.__floordiv__(y). But this may fail for two reasons:
- The method
x.__floordiv__()is not implemented in the first place, or
- The method
x.__floordiv__()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.__rfloordiv__() for reverse floor division 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.__floordiv__(x) instead of
x.__floordiv__(y), it could cause an error if the operation is non-commutative. That’s why
y.__rfloordiv__(x) is needed which indicates that floor division is possible after all.
So, the difference between
x.__rfloordiv__(y) is that the former calculates
x // y whereas the latter calculates
y // x — both calling the respective floor division method defined on object
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 __rfloordiv__(self, other): return 'called reverse floor division' x = Data_1() y = Data_2() print(x // y) # called reverse floor division
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. 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.