Polymorphism in action: an example using the __str__ method in Python

Опубликовано: 12 Март 2026
на канале: PatchyPython
59
1

In OOP, the same method call can produce different results according to the actual type of the object. This feature is known as polymorphism; it is based on method overriding, i.e. the re-definition of a method of a parent class in a child class. Here is an example using the simple class hierarchy we developed in the previous videos. The example also introduces the _str_ method, a special method that is automatically invoked for instance by the "print" statement to create a human-readable representation of the object for display purposes. Check out the notebooks linked below for more information on OOP in Python.

Embedded video, part of the following courses:
Computer Programming at Queen Mary, University of London
Jupyter notebook: https://tinyurl.com/yc7u9fsc
Coding for Scientists at Queen Mary, University of London
Jupyter notebook: https://tinyurl.com/4yhnhjn6

Notebooks will open on Google Colab - no installation required