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))

Output

Following is an output of the above code −

365 and 83 are equivalent to each other:False

Note − Because the numbers 365 and 83 are both integers, the == operator returns the right result in this case. However, the Python interpreter behaves differently when dealing with objects from unique classes.

Example

Let's say that the Square class has only one attribute, square, as shown below −

class Square: def __init__(self, area): self.square = area print ('equal')

Output

Following is an output of the above code −

equal

Now, the square attribute will be set to the same area in two instances of the class Square.

Example

class Square: def __init__(self, area): self.square = area sq1 = Square(23) sq2 = Square(23) print ('equal')

Output

Following is an output of the above code −

equal

Note − Even when both instances have the same area for the square attribute, the result of comparing the objects using the == operator will be False. This is shown in the following code −

Example

class Square: def __init__(self, area): self.square = area sq1 = Square(23) sq2 = Square(23) result = sq1 == sq2 print ('sq1 and sq2 are equal:',result)

Output

Following is an output of the above code −

sq1 and sq2 are equal: False

Note − The way the Python interpreter compares two objects from user-defined classes can be used to explain the behaviour shown above. The result will only be True if both objects point to the same memory address when we use Python's == operator to compare the equality of two class objects.

In other words, there will be just one Python object and two variables. This is seen in the example that follows −

Example

class Square: def __init__(self, area): self.square = area sq1 = Square(23) sq2 = sq1 result = sq1 == sq2 print ('sq1 and sq2 are equal:',result)

Output

Following is an output of the above code −

sq1 and sq2 are equal: True

It is now clear that the equality operator will only return True if both variables relate to the same instance of the user-defined class.

Equality using the __eq__() method

We can adjust the behavior of the == operator with respect to custom classes by overriding the __eq__() method. For instance, we can override the __eq__() function to compare the square of two instances of the Square class.

The following procedures will be applied inside the __eq__() method −

  • When used on a Square class instance, the __eq__() method will accept an additional object as an input argument.
  • We will first determine whether the input object is an instance of the Square class or not inside the __eq__() method. The isinstance() function can be used in this situation.
  • The class name and a Python object are the first and second input arguments, respectively, for the isinstance() function. If the object is an instance of the class specified in the input parameter after execution, it returns True.
  • In our program, the second input argument will be the Square class. It will return False if the first argument's object is not an instance of the Square class.

If not, we shall keep going,

  • We will compare the attribute square area in both objects in order to determine whether the two objects are of the same class. The result will be True if the areas are equal.

If not, we'll return False.

  • We can appropriately compare two instances of the Number class using the == operator once the __eq__() method is added to the Square class.
  • Assume we have two Square class instances, let's call them sq1 and sq2. The sq1.__eq__(sq2) function will be applied when sq1==sq2.
  • Similar to this, the sq2.__eq__(sq1) method will be executed when we conduct sq2==sq1.
  • Upon execution of the code, sq1==sq2 will return True if the square areas of both objects are equal. If not, False will be returned.

We can observe it in the following example −

Example

class Square: def __init__(self, area): self.square = area def __eq__(self, other): isSquare = isinstance(other, self.__class__) if not isSquare: return False if self.square == other.square: return True else: return False sq1 = Square(23) sq2 = Square(23) result = sq1 == sq2 print ('sq1 and sq2 are equal:',result)

Output

Following is an output of the above code −

sq1 and sq2 are equal: True

Equality using id() method

Additionally, you may determine whether or not two variables with objects of custom classes relate to the same object. The id() function can be used for this.

A unique identity number can be found anywhere in memory by using the id() function, which accepts an object as an input argument.

Example

Following is an illustration of id() method −

class Square: def __init__(self, area): self.square = area def __eq__(self, other): isSquare = isinstance(other, self.__class__) if not isSquare: return False if self.square == other.square: return True else: return False sq1 = Square(23) sq2 = Square(23) result1 = id(sq1) result2 = id(sq2) print ('The sq1 id is:',result1) print ('The sq2 id is:',result2)

Output

Following is an output of the above code −

The sq1 id is: 2632227365088
The sq2 id is: 2632227365232

Note − The id() function will return the same result for both objects if they both refer to the same memory location. We can determine whether or not two objects refer to the same memory address by comparing the output of the id() function as shown in the following example −

Example

class Square: def __init__(self, area): self.square = area def __eq__(self, other): isSquare = isinstance(other, self.__class__) if not isSquare: return False if self.square == other.square: return True else: return False sq1 = Square(23) sq2 = Square(23) result1 = id(sq1) result2 = id(sq2) result = result1 == result2 print ('sq1 and sq2 are equal:',result2)

Output

Following is an output of the above code −

sq1 and sq2 are equal: 2284484764016

As you can see, we didn't examine the objects' attribute areas to determine whether they were of the same class in this case.

In this instance, we are merely determining whether or not the objects refer to the same memory region. Therefore, using the == operator without including the __eq__() function in the class declaration is equivalent to utilising this way of determining Python class equality.

Updated on: 23-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements