Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between “__eq__” VS “is” VS “==” in Python

  • Last Updated : 01 Oct, 2020

There are various ways using which the objects of any type in Python can be compared. Python has “==” operator, “is” operator, and “__eq__” dunder method for comparing two objects of a class or to customize the comparison. This article explains the above said three operators and how they differ from each other.

== operator 

The “==” operator compares the value or equality of two objects, whereas the Python “is” operator checks whether two variables point to the same object in memory.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Example 1:



Python3




# lists initialization
x = [1, 2]
y = [1, 2]
  
z = y
  
# comparisons
print("x == y : ", x == y)
print("z == y : ", z == y)
  
# location in memory
print("Location of x is ", id(x))
print("Location of y is ", id(y))
print("Location of z is ", id(z))
Output
x == y :  True
z == y :  True
Location of x is  140169895643144
Location of y is  140169895642952
Location of z is  140169895642952

Example 2:

Python3




class Student:
    def __init__(self, name):
        self.name = name
  
  
divyansh = Student("Divyansh")
shivansh = Student("Divyansh")
  
print("divyansh == shivansh : ", (divyansh == shivansh))
Output
divyansh == shivansh :  False

In the above example, we should have got an output True, but instead, the output we received was False. This is because, for objects of user-defined classes, we need to specify how we are going to compare them. The Student object that we created has no idea on what is the criteria for two Students being equal, maybe they have the same attribute ‘name’, maybe they have a bunch of other features about them. What needs to be the same for us to say that one Student object is the same as another Student. Well, that’s what we are going to specify by ourselves by implementing a method there called __eq__. This is one of the dunder methods in Python. It allows us to override the basic functionality of “==” operator in Python. 

Example 3:

Python3




class Student:
    def __init__(self, name):
        self.name = name
  
    def __eq__(self, other):
        if isinstance(other, Student):
            if other.name == self.name:
                return True
        return False
  
  
divyansh = Student("Divyansh")
shivansh = Student("Divyansh")
  
print("divyansh == shivansh : ", (divyansh == shivansh))
Output
divyansh == shivansh :  True

Implementing this dunder method, “==” operator is used between two objects of Student class and get an accurate value comparison, now this is going to be different if I switch this to ‘is‘ operator.

Example 4:



Python3




class Student:
    def __init__(self, name):
        self.name = name
  
    def __eq__(self, other):
        if isinstance(other, Student):
            if other.name == self.name:
                return True
        return False
  
  
divyansh = Student("Divyansh")
shivansh = Student("Divyansh")
  
print("divyansh is shivansh : ", (divyansh is shivansh))
Output
divyansh is shivansh :  False

Here in the above code snippet, we received False as output, this is because divyansh is going to be different from the shivansh although these objects hold the same value. However, they have different memory locations i.e., they are different and unique objects that could change their properties at any point in time independently.

Example 5:

Python3




class Student:
    def __init__(self, name):
        self.name = name
  
    def __eq__(self, other):
        if isinstance(other, Student):
            if other.name == self.name:
                return True
        return False
  
  
divyansh = Student("Divyansh")
shivansh = divyansh
  
print("divyansh is shivansh : ", (divyansh is shivansh))
Output
divyansh is shivansh :  True

Now in the example above, the output is True, this is because of shivansh points to the same Student object that divyansh points to. 

‘is’ operator

The ‘is’ operator compares the identity of two objects. Here we compare the objects itself.

Example 6:

Python3




x = [1, 2]
y = [1, 2]
z = y
print("x is y : ", x is y)
print("z is y : ", z is y)
print("Location of x is ", id(x))
print("Location of y is ", id(y))
print("Location of z is ", id(z))
Output
x is y :  False
z is y :  True
Location of x is  139987316430856
Location of y is  139987316430664
Location of z is  139987316430664

In the example above, even if x and y have the same values, they store them in different locations in memory. So it is clear that even though the objects are the same in terms of the values their locations in memory are actually different. We also see that in the above snippet z is y gives True, this is because these objects are actually identical i.e., the location of y and location of z above are exactly the same.




My Personal Notes arrow_drop_up
Recommended Articles
Page :