- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between '__eq__' VS 'is' VS '==' in Python
The __eq__, ‘is’ and == operators in Python are used to compare the equality of objects in Python. The __eq__ method checks for the equality of objects of the same class or if we want to make a custom comparison function. The ‘is’ operator checks for the memory location of the two objects whereas the == operator checks only the value of the two objects being compared. In this article, we will discuss the difference between the three operators and their uses.
Checks for equality of objects values between two objects of the same class
Checks if two objects have the same memory location
a is b
Checks for the value of the two objects
The __eq__() method
The __eq__ method in Python is used to define how objects of a class are compared for equality. The __eq__ method takes two arguments: self(object on the left-hand side of == operator ) and other(object on the right-hand side of == operator). __eq__ method always returns a boolean value(True or False). If it returns something else other than the boolean value it will lead to TypeError.
In the below example, we create a class named Person which has two attributes namely name and age. We then define the __eq__() method in the class to compare the name and age attributes of the person object. Finally, two instances of the person class are created i.e p1 and p2 and they are compared using the == operator.
class Person: def __init__(self, name, age): self.name = name self.age = age def __eq__(self, other): if isinstance(other, Person): return self.name == other.name and self.age == other.age return False p1 = Person("John", 30) p2 = Person("John", 30) if p1 == p2: print("p1 and p2 are equal")
p1 and p2 are equal
The ‘is’ operator
The `is` operator checks the memory location of the two objects. If both the objects point to the same memory location then the `is` operator returns true else it returns false. The `is` operator is not used to checking the equality of the operator's value.
In the below example, we created an array and assigned it to another variable. With the help of is operator, we can check if both the variable a and b refers to the same memory location or not.
a = [1, 2, 3] b = a print(a is b)
The `==` operator
The `==` operator checks for the equality of values of the operands or objects being compared. It returns true if the operators have the same value and false otherwise. The `==` operator does not check for the memory location of the two objects.
In the below example, two arrays are created containing the same values. The == operator checks for the equality of two lists.
a = [1, 2, 3] b = [1, 2, 3] print(a == b) # True
In this article, we discussed the difference between the __eq__, is, and == operators in Python. __eq__ method compares the object of the same class for equality of values and the is operator checks for the memory location of the object being compared. The == operator checks for the equality of values of the two objects.
Kickstart Your Career
Get certified by completing the courseGet Started