Inheritance lets one class build on another. A subclass (or child) can use methods defined by a base class (or parent), and can add its own behavior. Choose it because callers can treat the child as the base, not merely to avoid copying a few lines.
class Reading:
def __init__(self, value):
self.value = value
def summary(self):
return f"Reading: {self.value}"
class TimedReading(Reading):
def __init__(self, value, minute):
super().__init__(value)
self.minute = minute
def time_note(self):
return f"Minute: {self.minute}"
sample = TimedReading(18, 5)
print(sample.summary())
print(sample.time_note())
Output:
Reading: 18
Minute: 5
class TimedReading(Reading) names the base. The child constructor uses super().__init__(value) to initialize value, then adds minute. Calling summary finds the inherited method; its self is still the sample object. A subclass with no constructor of its own can inherit the base constructor.
Reading: summary
^
| inherits from
TimedReading: time_note
In this simple single-base example super() finds the base implementation. More generally it follows Python’s method resolution order, which matters with multiple inheritance. The example assumes values and minutes are valid; a real contract should specify their allowed ranges.