Python __add__ Method Tutorial

  • Post author:
  • Post category:Python

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

What is __add__ Method in Python?

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

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

Python __add__ Method Syntax

def __add__(self, other):

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

Python __add__ Method Return Value

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

Example: using python __add__ method

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

    def __add__(self,other):
        return self.salary + other.salary


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

res = jack + john

print(res)

Output:

400000

As you can see, the `jack` and `john` objects are involved in an addition operation. So here because `jack` is the left-hand operand, then the `__add__` 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 as well.

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

So then the value of `salary` attribute for both objects is added together 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 (addition, 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 an addition, but you’re not under any obligation to add any value in the overloaded method! You can simply return a single value of any object type. 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 an addition operation.

Leave a Reply