__eq__
The dunder method that defines what == means for your objects; without it, two instances are equal only if they are the same object.
In more detail
Defining __eq__ removes the default __hash__, making instances unhashable until you define a matching __hash__. Objects that compare equal must have equal hashes.
Related terms
- __hash__ — The dunder method that returns an object's hash value, which lets it be a dictionary key or a set member.
- dunder method — A method whose name starts and ends with two underscores, such as __init__ or __len__, which Python calls for you to implement built-in behaviour.
- comparison operator — An operator that compares two values and gives true or false, such as equal, not equal, less than or greater than.
Taught in
- Dunder Methods, And Making Objects Pythonic — Hands-on Python: Complete Video Course & Book
- Inheritance, super() And Polymorphism — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Special Methods: __str__, __eq__ And Friends — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep