Part 4 · Section 05
Project 2
Time to put the special methods to work on a single, realistic class. In modular arithmetic, numbers “wrap around” a modulus, the way clock hours wrap around 12: 14 ≡ 2 (mod 12). In this project you’ll write a Mod class whose instances behave like numbers in that world: Mod(8, 3) is the same as Mod(2, 3), it equals the plain integer 5, and Mod(2, 3) + 2 is Mod(1, 3).
This project practices:
- validating constructor arguments and exposing read-only properties
__repr__and__int____eq__and__hash__based on congruence, and the limits of the hash contract when a class equals integers- arithmetic operators with
NotImplemented, reflected operators so that3 + Mod(1, 5)works, and three-argumentpow() - in-place operators that mutate the object, and why that conflicts with hashing
- ordering with
functools.total_ordering, and removing repetition with theoperatormodule