Python: The super() Function
November 19, 2019
In this video, we’ll learn why the
super() function is important in Python. You’ll also learn when and how to use it in your code.
super function returns a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class.
There are two typical use cases for
- In a class hierarchy with single inheritance,
supercan be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of
superin other programming languages.
The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).
Want to learn more? My recommendations:
Learn Python 3 the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code (Zed Shaw’s Hard Way Series)
Head First Python: A Brain-Friendly Guide
Introduction to Computing and Programming in Python (3rd Edition)