Method overriding defines a subclass method with the same signature as an inherited superclass method. At run time, polymorphism selects the subclass implementation for subclass instances. Use it when the subclass needs similar, but not identical, behavior. section 7.1.1 section 7.6
Design an override so that it preserves the inherited interface and remains substitutable:
- Match the method name, parameter count, parameter order, and return datatype; parameter names do not form part of the signature. section 7.1.1
- Call super() when the subclass behavior extends the superclass behavior instead of replacing its core calculation. section 7.1.1
- Keep subclass objects usable wherever superclass objects are expected; violating this substitution principle can cause logic errors or crashes. section 7.0 section 7.6
- Treat type hints as documentation and checking aids, not runtime barriers: Python can still override a method with incompatible hinted types. section 7.1.2
Extending an item cost
class Item: def _cost(self, price, weight): return price * weight class OrganicItem(Item): def __init__(self, markup): self._markup = markup def _cost(self, price, weight): total = super()._cost(price, weight) return total * (1 + self._markup / 100) Item calculates the regular price, while OrganicItem overrides _cost() and adds a markup. Its print_cost() method can likewise provide organic-specific output. section 7.1.1
A misleading type-hint override
class Superclass: def operate(self, x: str, y: str): return x + y class Subclass(Superclass): def operate(self, x: int, y: int): return x * y Subclass.operate() overrides Superclass.operate() despite the incompatible hints. Calling sub.operate('Hi, ', 'Ron') therefore selects multiplication and raises a TypeError. section 7.1.2


