__lt__
Dunder MethodPython 2.0+Intermediate
Defines behavior for the < less-than operator
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
class Student: def __init__(self, name, gpa): self.name = name self.gpa = gpa def __lt__(self, other): return self.gpa < other.gpa def __repr__(self): return f"{self.name}({self.gpa})" students = [Student("Alice", 3.8), Student("Bob", 3.5), Student("Charlie", 3.9)] print(sorted(students))
__lt__ defines behavior for the < less-than operator. Implementing it lets you customize how Python interacts with your objects.
Try in PlaygroundTags
oopmagic-methodprotocolcore