Python __mul__ Method Tutorial

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

What is __mul__ Method in Python?

Let’s say you have an object of type a class that you’ve created. This object is involved in a multiplication operation (on the left side of the multiplication `*` 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 `__mul__` method, we can override the multiplication operation for our custom objects.

Basically, when an object of custom type gets involved in a multiplication operation, the execution engine checks that object for the `__mul__` 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 multiplication operation.

Python __mul__ Method Syntax

def __mul__(self, other):

Python __mul__ 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 multiplication operation.
  2. The second argument is a reference to the right operand object in the multiplication operation.

Python __mul__ Method Return Value

The return value of the __mul__ 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 __mul__ method

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

    def __mul__(self,other):
        return self.age * other.age


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

res = jack * john

print(res)

Output:

2400

As you can see, the `jack` and `john` objects are involved in a multiplication operation. So here because `jack` is the left-hand operand, then the `__mul__` 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 `__mul__` method, we’ve decided to use the value of the `age` attribute as the value for the multiplication operation.

So then the value of `age` attribute for both objects is multiplied 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 (multiplication 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 multiplication, but you’re not under any obligation to multiply 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 multiplication operation.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies