Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference between \'__eq__\' VS \'is\' VS \'==\' in Python
In Python, object comparison can be performed using three different approaches: the __eq__ method, the is operator, and the == operator. Each serves a distinct purpose in determining equality or identity between objects.
Overview of Comparison Methods
| Method | Purpose | Checks | Usage |
|---|---|---|---|
__eq__ |
Custom equality logic | Object values (customizable) | Class method definition |
is |
Identity comparison | Memory location | a is b |
== |
Value comparison | Object values | a == b |
The __eq__() Method
The __eq__ method allows you to define custom equality logic for your classes. When you use the == operator on objects, Python internally calls the __eq__ method.
Example
Here's how to implement custom equality comparison for a Person class ?
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
# Create two Person objects with same attributes
p1 = Person("John", 30)
p2 = Person("John", 30)
print(f"p1 == p2: {p1 == p2}")
print(f"p1 is p2: {p1 is p2}")
p1 == p2: True p1 is p2: False
The 'is' Operator
The is operator checks if two variables point to the same object in memory. It compares object identity, not values.
Example
Here's how is works with different scenarios ?
# Same object reference
a = [1, 2, 3]
b = a
print(f"a is b: {a is b}")
# Different objects with same values
c = [1, 2, 3]
d = [1, 2, 3]
print(f"c is d: {c is d}")
print(f"c == d: {c == d}")
# Small integers are cached
x = 5
y = 5
print(f"x is y: {x is y}")
a is b: True c is d: False c == d: True x is y: True
The == Operator
The == operator compares values of objects. For built-in types, it checks if the values are equal. For custom classes, it calls the __eq__ method.
Example
Here's how == works with different data types ?
# Lists with same values
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(f"list1 == list2: {list1 == list2}")
# Strings with same content
str1 = "hello"
str2 = "hello"
print(f"str1 == str2: {str1 == str2}")
# Numbers
num1 = 42
num2 = 42
print(f"num1 == num2: {num1 == num2}")
list1 == list2: True str1 == str2: True num1 == num2: True
Key Differences in Practice
Let's see all three approaches in a single example ?
class Book:
def __init__(self, title):
self.title = title
def __eq__(self, other):
if isinstance(other, Book):
return self.title == other.title
return False
# Create book objects
book1 = Book("Python Guide")
book2 = Book("Python Guide")
book3 = book1
print("=== Comparison Results ===")
print(f"book1 == book2: {book1 == book2}") # Uses __eq__
print(f"book1 is book2: {book1 is book2}") # Different objects
print(f"book1 is book3: {book1 is book3}") # Same object
print(f"book1 == book3: {book1 == book3}") # Uses __eq__
=== Comparison Results === book1 == book2: True book1 is book2: False book1 is book3: True book1 == book3: True
Conclusion
Use __eq__ to define custom equality logic in your classes. Use is to check if two variables reference the same object. Use == to compare values, which internally calls __eq__ for custom objects.
