How do we use equivalence ("equality") operator in Python classes?

Using comparison operators, we may compare various data types in Python. When creating custom classes, we are unable to simply compare them using the comparison operators.

This article will go over various approaches to verify equivalence ("equality") in Python classes.

Equality of Class Objects

The == operator makes it simple to determine whether two built-in objects, such as strings or integers, are equal. This is demonstrated in the example below.

Example

Following is an example of == operator −

char1 = 365
char2 = 83
result = char1 == char2
print("{} and {} are equivalent to each other: {}".format(char1, char2, result))
365 and 83 are equivalent to each other: False

Because the numbers 365 and 83 are both integers, the == operator returns the correct result. However, the Python interpreter behaves differently when dealing with objects from custom classes.

Example

Let's create a Square class with a single attribute, area

class Square:
    def __init__(self, area):
        self.area = area

sq1 = Square(23)
sq2 = Square(23)
result = sq1 == sq2
print("sq1 and sq2 are equal:", result)
sq1 and sq2 are equal: False

Even when both instances have the same area attribute, the result of comparing the objects using the == operator is False. This happens because Python compares object identity (memory location) by default, not their attribute values.

The equality operator will only return True if both variables refer to the same instance −

Example

class Square:
    def __init__(self, area):
        self.area = area

sq1 = Square(23)
sq2 = sq1  # Same object reference
result = sq1 == sq2
print("sq1 and sq2 are equal:", result)
sq1 and sq2 are equal: True

Equality Using the __eq__() Method

We can customize the behavior of the == operator for custom classes by overriding the __eq__() method. This allows us to define what "equal" means for our objects.

The __eq__() method should −

  • Accept another object as a parameter
  • Check if the other object is an instance of the same class using isinstance()
  • Compare the relevant attributes to determine equality
  • Return True if objects are equal, False otherwise

Example

class Square:
    def __init__(self, area):
        self.area = area
        
    def __eq__(self, other):
        if not isinstance(other, Square):
            return False
        return self.area == other.area

sq1 = Square(23)
sq2 = Square(23)
sq3 = Square(25)

print("sq1 and sq2 are equal:", sq1 == sq2)
print("sq1 and sq3 are equal:", sq1 == sq3)
sq1 and sq2 are equal: True
sq1 and sq3 are equal: False

Equality Using id() Method

The id() function returns a unique identifier for each object in memory. You can use it to check if two variables refer to the exact same object.

Example

class Square:
    def __init__(self, area):
        self.area = area

sq1 = Square(23)
sq2 = Square(23)
sq3 = sq1

print("sq1 id:", id(sq1))
print("sq2 id:", id(sq2))
print("sq3 id:", id(sq3))

print("sq1 and sq2 same object:", id(sq1) == id(sq2))
print("sq1 and sq3 same object:", id(sq1) == id(sq3))
sq1 id: 2284484764016
sq2 id: 2284484764160
sq3 id: 2284484764016
sq1 and sq2 same object: False
sq1 and sq3 same object: True

Using id() comparison is equivalent to using the default == operator behavior without implementing __eq__() − it only checks object identity, not attribute values.

Comparison

Method Compares Use Case
Default == Object identity Check if variables refer to same object
Custom __eq__() Attribute values Define logical equality based on content
id() comparison Memory address Explicit identity checking

Conclusion

Override the __eq__() method to define meaningful equality for custom classes based on attribute values. Use id() when you need to check if two variables reference the same object in memory.

Updated on: 2026-03-24T19:45:01+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements