super()
A built-in that lets a method call the version of itself in the parent class, most often super().__init__(...) in an initialiser.
In more detail
It follows the method resolution order, so it works correctly even with several parent classes. Without it, a subclass initialiser that does its own setup would skip the parent's setup entirely.
Example
class Puppy(Dog):
def __init__(self, name):
super().__init__(name)
self.age = 0Related terms
- inheritance — Declaring one class or type as a specialised version of another, so it receives the parent's members and can be used wherever the parent is expected.
- method overriding — Defining a method in a subclass with the same name as one in its parent, so the subclass's version is used instead.
- __init__ — The dunder method Python calls to set up a newly created instance, receiving the arguments passed to the class.
Taught in
- Inheritance And Composition — Hands-on Python: Complete Video Course & Book
- Exception Classes Of Your Own — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exceptions Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Introspection: hasattr, __name__, __module__ And __bases__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Inheritance, super() And Polymorphism — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Multiple Inheritance, The MRO And Diamonds — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep