# Principled Programming / Tim Teitelbaum / Chapter 18. from pair import Pair # Fraction class Fraction(Pair[int, int]): """Fraction using generic pairs instantiated for ints.""" # Constructor. def __init__(self, numerator: int, denominator: int) -> None: super().__init__(numerator, denominator) # Apply the Pair constructor. assert denominator != 0, "0 denominator" # Access. def get_numerator(self) -> int: return self._key def get_denominator(self) -> int: return self._value # String representation. def __str__(self) -> str: return str(self._key) + "/" + str(self._value) # Equality. def __eq__(self, other) -> bool: if other is None: return False if other is self: return True if not isinstance(other, Fraction): return False; return (self._key == other._key) and (self._value == other._value)