PythiloLesson 7 of 30 · progress kept in this browser

Equality, hashing and ordering

Without help, == on your objects compares identity. Define __eq__ to compare values. For a type you do not handle, return NotImplemented: Python then tries the other operand's method, and for == finally falls back to identity.

from functools import total_ordering

@total_ordering
class Money:
    def __init__(self, cents):
        self.cents = cents

    def __eq__(self, other):
        if not isinstance(other, Money):
            return NotImplemented
        return self.cents == other.cents

    def __lt__(self, other):
        if not isinstance(other, Money):
            return NotImplemented
        return self.cents < other.cents

    def __hash__(self):
        return hash(self.cents)

print(Money(5) == Money(5))
print(Money(5) > Money(2))
print(len({Money(5), Money(5)}))
Your task

Write Version(text) storing text and parts, the tuple of ints from a string like "1.10.2". Versions compare by parts with all six comparison operators, work as set members and dict keys, return NotImplemented against other types, and repr as Version('1.10.2').

Next: Properties