Python __rlshift__() Magic Method

Rate this post


object.__rlshift__(self, other)

The Python __rlshift__() method implements the reverse bitwise left-shift operation with reflected, swapped operands. So, when you call x << y, Python attempts to call x.__lshift__(y). If the method is not implemented, Python attempts to call y.__rlshift__(x) on the right operand and if this isn’t implemented either, it raises a TypeError.

The __rlshift__() method is often used in practice to add the left-shift functionality to a class that doesn’t yet provide it and that cannot be modified such as a list or a built-in data type. By implementing the method for the other operand, you can still use the left-shift functionality for the class that doesn’t implement __lshift__().

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 Bitwise Left-Shift

The Python __lshift__() method implements the built-in << operation. So, when you cal x << y, Python attempts to call x.__lshift__(y).

For a positive integer, it inserts a 0 bit on the right and shifts all remaining bits by one position to the left. For example, if you left-shift the binary representation 0101 by one position, you’d obtain 01010. Semantically, the bitwise left-shift operator x << n is the same as multiplying the integer x with 2**n.

print(8 << 1)
# 16

print(8 << 2)
# 32

print(-3 << 1)
# -6

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

Python Bitwise Left-Shift Operator

Python __lshift__ vs __rlshift__

Say, you want to calculate the left-shift operation on two custom objects x and y:

print(x << y)

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

  1. The method x.__lshift__() is not implemented in the first place, or
  2. The method x.__lshift__() 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.__rlshift__() for reverse left-shift on the right operand y. Not that this is not the same as > Operator”>right-shift, it just means that the left-shift operation is called on the second operand y.

If the reverse left-shift 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.__lshift__(x) instead of x.__lshift__(y), the result would be wrong because the operation is non-commutative. That’s why y.__rlshift__(x) is needed.

So, the difference between x.__lshift__(y) and x.__rlshift__(y) is that the former calculates x << y whereas the latter calculates y << x — both calling the respective method defined on the 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 __rlshift__(self, other):
        return 'called reverse lshift'

x = Data_1()
y = Data_2()

print(x << y)
# called reverse lshift


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!