The __eq__()
method is a special method that allows objects of the same or compatible classes to compare each other for equality. This is the method that gets called when the equality comparison ==
operator is used to compare objects. So if you have ever used the equality operator(==
), you have also indirectly used the __eq__()
method.
#Example with strings
S1 = "Hello"
S2 = "hello".capitalize()
print(S1.__eq__(S2)) #This is the indirect way for print(S1==S2)
#Example with integers
print(int(10).__eq__(5 * 2)) #Same as print(10 == 5 *2)
#Example with lists
L1 = [1, 2, 3]
L2 = [4, 5, 6]
print(L1.__eq__(L2)) #Same as print(L1 == L2)
User-defined objects can override this method and implement their own comparison logic i.e. logic to decide whether two objects should be regarded as equal.
Overriding the __eq__() method for user-defined objects
If the __eq__()
method is not defined on a class, two objects will be regarded as not equal. For example:
class Example:
pass
e1 = Example()
e2 = Example()
print(e1 == e2)#returns False
But when we define the __eq__()
method, two objects will be checked based on the comparison logic that we implement. This helps to standardize the way objects of a class will be compared to each other before concluding whether they are equal or not.
def __eq__(self, other):
#method body
This function should include only two required parameters, self
to represent the current object, and other
to represent the other object to be compare
The __eq__()
method should return a boolean value indicating whether the two objects(self
and other
) are equal. True
if they are equal, False
if not.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
if isinstance(other, Point):
return (self.x, self.y) == (other.x, other.y) #Returns True if both x and y of the two objects are equal
return False
#Instantiate objects
p1 = Point(5, 7)
p2 = Point(4, 6)
p3 = Point(5, 7)
#Check objects for equality
print(p1 == p2)
print(p1 == p3)
print(p2 == p3)
In the above example we are able to make the comparison criteria more logical for class Point
's instances. If we hadn't implemented the __eq__()
method, the ==
operator would have returned False
regardless of whether both x
and y
are equal for the objects.
Note: It is necessary to confirm that the "other" object is actually an instance of the class. For example in the above case we used the builtin isinstance() function to check that the other object being compared is a Point instance.
This avoids any confusion when comparing objects that happen to have the same attributes but are from different classes. If an object is not an instance of the class being compared, then comparisons may not make sense and may lead to illogical and incorrect results.
Another Example:
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
def __eq__(self, other):
if isinstance(other, Book):
if other.title == self.title and other.author == self.author and other.isbn == self.isbn:
return True
return False
book1 = Book("The Cat in the Hat", "Dr. Seuss", "123456")
book2 = Book("The Cat in the Hat", "Dr. Seuss", "123456")
book3 = Book("Book Thief", "Markus Zusak", "987654")
print(book1 == book2)
print(book2 == book3)
Conclusion:
- The
__eq__()
method checks whether two objects are equal or not. If equal it returnsTrue
andFalse
otherwise. - All Python objects have the
__eq__()
method defined by default. This makes it possible to use the comparison operator with any objects. - If the
__eq__()
method is not defined, two objects of the same classes will be regarded as not equal. - User-defined objects can override the
__eq__()
method and implement their own comparison logic. - The
__eq__()
should take only two required arguments i.e the current object(self
) and the other object(other
).