__rshift__() method implements the built-in
>> operation. So, when you cal
x >> y, Python attempts to call
x.__rshift__(y). If the method is not implemented, Python first attempts to call
__rrshift__ 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.
The Python bitwise right-shift operator
x >> n shifts the binary representation of integer
n positions to the right.
It inserts a
0 bit on the left and removes the right-most bit. For example, if you right-shift the binary representation
0101 by one position, you’d obtain
0010. Semantically, the bitwise right-shift operator is the same as performing integer division by
print(8 >> 1) # 4 print(8 >> 2) # 2 print(-3 >> 1) # -2
To understand this operation in detail, feel free to read over > Operator”>our tutorial or watch the following video:
Example Custom __rshift__()
In the following example, you create a custom class
Data and overwrite the
__rshift__() method so that it returns a dummy string when trying to calculate the bitwise right-shift operation.
class Data: def __rshift__(self, other): return '... my result of rshift...' a = Data() b = Data() print(a >> b) # ... my result of rshift...
If you hadn’t defined the
__rshift__() method, Python would’ve raised a
TypeError: unsupported operand type(s) for >>
Consider the following code snippet where you try to calculate the right-shift operation on custom objects without defining the dunder method
class Data: pass a = Data() b = Data() print(a >> b)
Running this leads to the following error message on my computer:
Traceback (most recent call last): File "C:UsersxcentDesktopcode.py", line 8, in <module> print(a >> b) TypeError: unsupported operand type(s) for >>: 'Data' and 'Data'
The reason for this error is that the
__rshift__() 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
__rshift__(self, other) method in your class definition as shown previously:
class Data: def __rshift__(self, other): return '... my result of rshift...'
Of course, you’d use another return value in practice as explained in the “Background rshift()” section.
Python __rshift__ vs __rrshift__
print(x >> y)
class Data_1: pass class Data_2: def __rrshift__(self, other): return 'called reverse rshift' x = Data_1() y = Data_2() print(x >> y) # called reverse rshift
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.