Lesson 16: More magic methods — __eq__ and __len__
In lesson 5 you discovered __str__ teaches print to talk to your object. Today we extend the magic: __eq__ teaches == to compare objects by content, and __len__ teaches len() to measure them. Your objects become first-class citizens of the language.
Two 100-shekel bills are "equal" even though they're different pieces of paper — __eq__ teaches Python to compare like that: by value, not by paper.
- __eq__
- A magic method running automatically on == comparison — defining when two objects count as equal.
- __len__
- A magic method running automatically on len() — defining what the object's "length" is.