The __eq__ Method in Python
This guide will show how we handle the __eq__()
method in Python. We’ll comp different objects and learn the functionality of this particular method. Let’s dive right into it.
__eq__()
in Python
First, __eq__()
is a dunder/magic method, as you can see that it is preceded and succeeded by double underscores. Whenever you use the ==
operator, this method is already called to compare two class instances. Python will use the is
operator for comparison if you don’t provide a specific implementation for the __eq__()
method. But how do you specify this method? The answer to that lies in overriding this function. Take a look at the following code.
# __eq__ this is called dunder/magic method in python
# class of student has constructor with two arguments
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
# object one of class student
s1 = Student("David", 293)
# object two of class student
s2 = Student("David", 293)
# Applying equality function two objects
print(s1 == s2)
Output:
False
As you can see, we created a class named Student
and made two of its arguments as name
, and roll
. Later, you created two instances featuring the same Student
class data. Comparing these two instances as we have done in the above code will give a false
output.
Despite these instances having the same data, they are not equal once compared. It is because the ==
operator compares the address of the cases, which is different. You can see that by running the following code and the above code.
# Both objects have different ids thats why the s1==s2 is false
print(id(s1))
print(id(s2))
Output:
1991016021344 //(it will vary)
1991021345808 //(it will vary)
But if we were to create an instance and later save that instance in another example like the following, their addresses would be the same. Take a look.
# object one of class student
s1 = Student("Ahsan", 293)
s3 = s1
# In this case we have same id for s1 and s3
print(s1 == s3)
print(id(s1))
print(id(s3))
Output:
True
1913894976432 //(it will vary)
1913894976432 //(it will vary)
It would give you true
as output. So, how can we compare the two instances based on the data? We need to implement the dunder method and override it like the following.
# class of student has constructor with two arguments and a modified dunder method of __eq__
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
# implementing the dunder method
def __eq__(self, obj):
# checking both objects of same class
if isinstance(obj, Student):
if self.roll == obj.roll:
return True
else:
return False
# object of class student
s4 = Student("David", 293)
# other object of class student
s5 = Student("David", 293)
# Applying equality function two objects
print(s4 == s5)
# Both objects have different ids, but the s4==s5 is true because we override the __eq__ function it will give true when the roll number is some
print(id(s4))
print(id(s5))
# object of class student
s6 = Student("David", 293)
# other object of class student
s7 = Student("David", 178)
# Applying equality function two objects will give us False because the roll is different
print(s6 == s7)
Output:
True
1935568125136 //(it will vary)
1935568124656 //(it will vary)
False
Now, we are overriding the __eq__()
method by putting an if
condition inside that checks whether the roll
is the same or not. That’s how you override an __eq__()
method in Python.
Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.
LinkedIn