Python __eq__ Method Tutorial

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

What is __eq__ Method in Python?

The equal operator `==` by default is capable of taking basic data types like `str`, `int`, `float` etc. as its operands and returning the result of whether they are equal in value.

But if the values involved in the operation were of custom data types (For example, a custom class that we’ve created), then with a high chance the comparison will result inaccurate! Because python doesn’t know based on the value of what attribute, it should run the comparison!

Now, in order to make a custom object get involved in a comparison operation and run an accurate comparison based on the requirement of our program, Python has provided a method called __eq__.

If we override this method in a class and create an object from it, then the python execution engine will call this method automatically when the target object gets involved in a comparison operation. Note that the target object should be declared as the left-hand operator so that Python can call this method! This means if the object is the right-hand operand in the comparison, then the method won’t get called.

Python __eq__ Method Syntax

def __eq__(self, other):

Python __eq__ Method Parameters

The method has two parameters and they are filled automatically by Python interpreter behind the scene when the target object is involved in the comparison operation.

  • The first parameter of this method is a reference to the object that is involved in a comparison operation (the one that is set as the left-hand operand in the comparison).
  • The second parameter is a reference to the right-hand operand involved in the comparison operation.

Python __eq__ Method Return Value

The return value of this method typically should be a boolean value! But there’s no obligation and so the return value could be of any data type!

Example: using python __eq__ method

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

    def __eq__(self,other):
        return self.salary == other.salary 

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

if jack == john:
    print("Both employees have the same salary!")
    print("These two employees don’t have the same salary.")


Both employees have the same salary

In this example, we’ve overloaded the `__eq__` method in the `Employee` class. We then use the value of the `salary` attribute as the measurement of comparison within the body of this method.

So when an object of type Employee gets involved in an equal operation (as the Left operand) the Python execution engine (aka the interpreter) will call the `__eq__` method and passes the arguments and runs the body of the method to get the final value.

Note that this example considered the right operand involved in the comparison operation as an object of type Employee, but this is not an obligation and it’s just a design decision! Basically, based on your requirements for a class, the `other` parameter of the `__eq__` method could be of any type! Also remember that the return value of this method could also be of anything of any type.


Top Technologies