In this section we will learn what the __truediv__ method is and how to use it in Python.
What is __truediv__ Method in Python?
Let’s say you have an object of type a class that you’ve created. This object is involved in a division operation (on the left side of the 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 `__truediv__` method, we can override the division operation for our custom objects.
Basically, when an object of custom type gets involved in a division operation, the execution engine checks that object for the `__truediv__` 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 division operation.
Python __truediv__ Method Syntax
def __truediv__(self, other):
Python __truediv__ Method Parameters
The method takes two arguments and both of them are filled automatically by the Python execution engine:
- The first argument is a reference to the object itself when the object is involved as the left operand in a division operation.
- The second argument is a reference to the right operand object in the division operation.
Python __truediv__ Method Return Value
The return value of the __truediv__ 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 __truediv__ method
class Employee: def __init__(self, age, salary): self.age = age self.salary = salary def __truediv__(self,other): return self.salary / other.salary jack = Employee(40, 300000) john = Employee(60, 250000) res = jack / john print(res)
As you can see, the `jack` and `john` objects are involved in a division operation. So here because `jack` is the left-hand operand, then the `__truediv__` 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 `__truediv__` 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 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 (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 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 division operation.