Python __sub__ Method Tutorial

  • Post author:
  • Post category:Python

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

What is __sub__ Method in Python?

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

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

Python __sub__ Method Syntax

def __sub__(self, other):

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

Python __sub__ Method Return Value

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

Example: using python __sub__ method

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

    def __sub__(self,other):
        return self.salary - other.salary


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

res = jack - john

print(res)

Output:

- 100000

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

So then the value of `salary` attribute for both objects is subtracted from 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 (subtraction, 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 subtraction, but you’re not under any obligation to subtract 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 the subtraction operation.

Leave a Reply