Python __floordiv__ Method Tutorial

In this section, we will learn what the __floordiv__ method is and how to use it in Python.

What is __floordiv__ Method in Python?

Let’s say you have an object of type a class that you’ve created. This object is involved in a floor division operation (on the left side of the floor division `//` operator). Now the question is, how does python know what value to take from this custom object? For example, let’s say the object has two attributes named `age` and `salary` and both have integer values. So does python take the value of the `age` attribute? Is it the `salary` attribute? Does it use both values in the operation?

Well, the answer is none of them! Actually Python will return an error by default because it doesn’t know what to do with an object of custom type!

But using the `__floordiv__` method, we can override the floor division operation for our custom objects.

Basically, when an object of custom type gets involved in a floor division operation, the execution engine checks that object for the `__floordiv__` method to see if this method is a member of the object or not. If it was, it will then call the method to get its result. It will then use that result as the value in the floor division operation.

Python __floordiv__ Method Syntax

def __floordiv__(self, other):

Python __floordiv__ Method Parameters

The method takes two arguments and both of them are filled automatically by the Python execution engine:

  1. The first argument is a reference to the object itself when the object is involved as the left operand in a floor division operation.
  2. The second argument is a reference to the right operand object in the floor division operation.

Python __floordiv__ Method Return Value

The return value of the __floordiv__ method is something that we as developers define what it should be. For example, it can be an integer, a float object, a List, a Dictionary, etc.

Example: using python __floordiv__ method

class Employee: 
    
    def __init__(self, age, salary):
        self.age = age
        self.salary = salary

    def __floordiv__(self,other):
        return self.salary // other.salary


jack = Employee(40, 300000)
john = Employee(60, 250000)

res = jack // john

print(res)

Output:

1

As you can see, the `jack` and `john` objects are involved in a floor division operation. So here because `jack` is the left-hand operand, then the `__floordiv__` method of this object will be called and a reference of this object will be passed to the `self` parameter and also a reference of the right-hand operand (john) will be passed to the `other` parameter.

Within the body of the `__floordiv__` method, we’ve decided to use the value of the `salary` attribute as the value for the division operation.

So then the value of `salary` attribute for both objects is floor divided by each other and the result returned and assigned to the `res` variable.

Note: the value of the `other` parameter could be anything from an object of basic type to an object of the same type, just like the example above. The important thing to remember is that the right-hand operand of the target operation (floor division, for this example) will be assigned to the `other` parameter. So it’s all about your design and requirement for how you want to interact with the value of the `other` parameter and what should be returned from the method.

For example, the operation is said to be floor division, but you’re not under any obligation to divide any value in the overloaded method! You can simply return a single value of any object. Or you can subtract values from each other and return that as a result! Or even return a string value as a result of the method! So it’s all about what you want to do with your objects when they are involved in a floor division operation.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies