Difference between “__eq__” VS “is” VS “==” in Python
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.
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
x == y : True z == y : True Location of x is 140169895643144 Location of y is 140169895642952 Location of z is 140169895642952
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.
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.
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.
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.
The ‘is’ operator compares the identity of two objects. Here we compare the objects itself.
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.