Inheritance
Inheritance lets a class reuse and override the behaviour of another class. class PriorityOrder(Order): is a subclass: it is an Order, plus whatever it adds or replaces.
Detailed Explanation
When you look up an attribute on an instance, Python searches the instance, then the class, then the class's bases in method resolution order. Inheritance is that search. A subclass that does not define total uses the parent's total. A subclass that defines total hides the parent's, unless it calls super().total().
super() returns a proxy that continues the MRO search after the current class. In a single-inheritance tree it means "the parent". In a multiple-inheritance tree it means "the next class", which may not be the one you listed first. Always call super() with no arguments inside a method; the compiler fills in the class and instance.
A subclass is a subtype for isinstance and issubclass. isinstance(PriorityOrder(...), Order) is True. That is the point of inheritance as a modelling tool: callers that accept an Order can receive a PriorityOrder and still call total.
Inheritance is the wrong tool when you only want to reuse a helper. A function, or composition (self.inner = Order(...)), does not create a subtype. "Is-a" is the test. A PriorityOrder is an Order. A OrderExporter has an Order. Subclassing to borrow two methods produces a brittle type that cannot change independently.
Python allows inheriting from builtins (list, dict, str, Exception). Exception hierarchies are the healthy case. Subclassing list to add domain methods often fights the builtin constructors (list("abc") iterates characters). Prefer composition around a list attribute, or collections.UserList when you truly need a list subtype.
Multiple inheritance, mixins, and MRO have their own entries. For everyday code, one base class and super().__init__(...) is the whole skill.
Code Examples
class Order:
def __init__(self, sku: str, qty: int):
self.sku = sku
self.qty = qty
def label(self) -> str:
return f"{self.qty}×{self.sku}"
class PriorityOrder(Order):
def __init__(self, sku: str, qty: int, priority: int):
super().__init__(sku, qty)
self.priority = priority
def label(self) -> str:
return f"P{self.priority} {super().label()}"
line = PriorityOrder("A-1", 2, 1)
print(line.label()) # P1 2×A-1
print(isinstance(line, Order)) # Trueprint(issubclass(PriorityOrder, Order)) # True
print(PriorityOrder.__mro__)Common Gotchas
Forgetting `super().__init__`
parent attributes never get set. Call `super()` unless you replace the entire initialiser.
Overriding without calling `super` drops parent behaviour
that is correct when you replace it, and a bug when you meant to extend it.
Inheritance copies the coupling, not just the code
a change to the parent changes every subclass. Prefer composition when the relationship is "uses", not "is".
`super()` in a `classmethod` still works
it passes the class along the MRO, which is why alternative constructors stay subclass-friendly.