Comparison magic methods let you decide how objects compare to each other using operators like <, >, ==, and more.
Comparison magic methods in Python
Start learning this pattern below
Jump into concepts and practice - no test required
def __eq__(self, other): # return True if self equals other def __lt__(self, other): # return True if self is less than other def __le__(self, other): # return True if self is less than or equal to other def __gt__(self, other): # return True if self is greater than other def __ge__(self, other): # return True if self is greater than or equal to other def __ne__(self, other): # return True if self is not equal to other
Each method should return a boolean (True or False).
Use these methods inside your class to customize how comparison operators work.
def __eq__(self, other): return self.value == other.value
def __lt__(self, other): return self.value < other.value
def __ne__(self, other): return self.value != other.value
This program creates a Box class with a volume. It compares boxes by their volume using ==, <, and > operators.
class Box: def __init__(self, volume): self.volume = volume def __eq__(self, other): return self.volume == other.volume def __lt__(self, other): return self.volume < other.volume def __gt__(self, other): return self.volume > other.volume box1 = Box(10) box2 = Box(20) box3 = Box(10) print(box1 == box2) # False print(box1 == box3) # True print(box1 < box2) # True print(box2 > box3) # True
Only __eq__ and __ne__ are required for equality checks; others are for ordering.
If you define __eq__, also define __ne__ for consistent behavior.
Python can use functools.total_ordering to fill in missing comparison methods if you define __eq__ and one ordering method.
Comparison magic methods let you control how objects compare with ==, <, >, etc.
Define these methods inside your class to customize comparisons.
They return True or False based on your rules.
Practice
==?Solution
Step 1: Understand the equality operator
The==operator checks if two objects are equal.Step 2: Identify the corresponding magic method
In Python,__eq__is the method that defines equality behavior.Final Answer:
__eq__ -> Option AQuick Check:
Equality operator uses __eq__ [OK]
- Confusing __eq__ with __lt__ or __gt__
- Thinking __ne__ handles equality
- Mixing up method names with comparison operators
< in a Python class?Solution
Step 1: Recall the magic method name for <
The method for < is__lt__and it takes two parameters: self and other.Step 2: Check method signature correctness
Correct syntax isdef __lt__(self, other):. Other options have wrong names or missing parameters.Final Answer:
def __lt__(self, other): -> Option DQuick Check:
Less than operator uses __lt__(self, other) [OK]
- Using wrong method names like __less_than__
- Omitting the other parameter
- Using incorrect method signatures
class Number:
def __init__(self, value):
self.value = value
def __gt__(self, other):
return self.value > other.value
n1 = Number(5)
n2 = Number(3)
print(n1 > n2)Solution
Step 1: Understand the __gt__ method
The__gt__method comparesself.valueandother.value.Step 2: Evaluate the comparison
n1.value is 5 and n2.value is 3, so 5 > 3 is True.Final Answer:
True -> Option CQuick Check:
5 > 3 = True [OK]
- Forgetting to compare attributes inside objects
- Expecting print to show object addresses
- Confusing __gt__ with __lt__
!=:
class Item:
def __init__(self, val):
self.val = val
def __ne__(self):
return self.val != other.valSolution
Step 1: Check __ne__ method signature
The__ne__method must take two parameters: self and other.Step 2: Identify missing parameter
Here,otheris used but not declared as a parameter, causing an error.Final Answer:
__ne__ method is missing the other parameter -> Option BQuick Check:
__ne__ needs (self, other) parameters [OK]
- Omitting the other parameter in comparison methods
- Misunderstanding method signatures
- Thinking inheritance from object is required in Python 3
Box where two boxes are considered equal if their volumes are equal. Which magic method should you implement and how?
class Box:
def __init__(self, length, width, height):
self.length = length
self.width = width
self.height = height
# Your code hereSolution
Step 1: Identify the comparison needed
Equality means using==, so implement__eq__.Step 2: Define volume comparison inside __eq__
Calculate volume for both boxes and compare for equality.Final Answer:
Implement __eq__(self, other) to compare volumes: return self.length * self.width * self.height == other.length * other.width * other.height -> Option AQuick Check:
Equality uses __eq__ comparing volumes [OK]
- Using __lt__ or __gt__ for equality
- Not comparing volumes but attributes directly
- Forgetting to implement __eq__ for == operator
