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.
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:
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
copy
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.
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:
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
).