Inheritance
Inheritance means: a new class takes over everything from an existing one and adds only what is different. You will learn the three building blocks one by one: inheriting, super() and overriding.
Module 1 — Why Inheritance?
Do not write everything again.
Base class and subclass
A temperature experiment is a measurement series — with name, readings and mean — plus a little more. Instead of writing everything again, the new class inherits from the existing one. The existing one is called the Basisklasse, the new one the Unterklasse.
class TemperaturVersuch(Messreihe):
pass
t = TemperaturVersuch("Erwärmung", [20, 24, 22])
print(t.mittelwert())
The base class goes in brackets after the name. Even with just pass the subclass can do everything the base class can — mittelwert() is inherited.
Example 1.1 — Inherited method
Loading Python…Tests
Module 2 — Own Attributes with super()
Let the base class initialise, then add your own.
super().__init__(...)
If the subclass needs an additional attribute, it gets its own constructor. So that name and readings are still stored as usual, it first calls the constructor of the base class — via super(). After that come its own attributes.
class TemperaturVersuch(Messreihe):
def __init__(self, name, messwerte, einheit="°C"):
super().__init__(name, messwerte)
self.einheit = einheit
Without the super()call there would be neither name nor messwerte in the object — the base class is not initialised automatically.
Example 2.1 — Base class plus own attribute
Loading Python…Tests
Module 3 — Overriding Methods
The subclass may do it differently.
Same name, new version
If the subclass writes a method with the same name as the base class, its objects use the new version. This is called overriding. A typical case is its own __str__, so that the temperature experiment introduces itself differently from a general measurement series.
def __str__(self):
return f"Temperaturversuch {self.name}: {len(self.messwerte)} Werte"
With issubclass(TemperaturVersuch, Messreihe) you check whether one class inherits from another; isinstance(t, Messreihe) tells you whether an object belongs to it (also as a subclass).
Example 3.1 — Inherited or overridden?
Loading Python…Tests
Module 4 — From Measurement Object to Experiment
The pattern behind the exercises.
Shared things go in the base class, special things in the subclass
This gives a small data model for the lab: Messreihe can do everything every measurement needs. TemperaturVersuch adds the unit and the highest temperature. A pressure series adds limits and checks whether all values lie between them. None of it is written twice.
PracticeNow Program It Yourself
Three exercises: TemperaturVersuch inherits from Messreihe, gets its own __str__, and finally a pressure series with plausibility limits is created.
Example 4.1 — Two subclasses, one base
Loading Python…Tests
Sources and Further Reading
Module sources and further Python references.
Teaching Material and Documentation
- Lecture notebook IngInfo_WS26_27_VÜ.ipynb, section on object-oriented programming.
- Python tutorial — Classes.
- Real Python — Object-Oriented Programming in Python.
- Theis, Thomas: Getting started with Python. 7th edition, Rheinwerk Verlag, 2023.