One of the core features of Python is its OOP paradigm, which you can use to model real-world entities and their relationships.

When working with Python classes, you’ll often use inheritance and override a superclass’s attributes or methods. Python provides asuper()function that lets you call the superclass’s methods from the subclass.

using super in class constructor

What Is super() and Why Do You Need It?

Using inheritance, you can make a new Python class that inherits the features of an existing class. You can also override methods of the superclass in the subclass, providing alternative implementations. However, you might want to use the new functionality in addition to the old, rather than instead of it. In this case,super()is useful.

You can use thesuper()function to access superclass attributes and invoke methods of the superclass. Super is essential forobject-oriented programmingbecause it makes it easier to implement inheritance and method overriding.

trying to use parent attributes without super

How Does super() Work?

Internally,super()is closely related to theMethod Resolution Order(MRO) in Python, which the C3 Linearization algorithm determines.

Here’s howsuper()works:

Using super() in a Class Constructor

Usingsuper()in a class constructor is common practice, since you’ll often want to initialize common attributes in the parent class and more specific ones in the child.

To demonstrate this,define a Python class,Father, which aSonclass inherits from:

Inside theSonconstructor, the call tosuper().init()invokes theFatherclass constructor, passing itfirst_nameandlast_nameas parameters. This ensures that theFatherclass can still set the name attributes correctly, even on aSonobject.

If you do not callsuper()in a class constructor, the constructor of its parent class will not run. This can lead to unintended consequences, such as missing attribute initializations or incomplete setup of the parent class’s state:

using super in class method

If you now try to call theget_infomethod, it will raise anAttributeErrorbecause theself.first_nameandself.last_nameattributes have not been initialized.

Using super() in Class Methods

you’re able to usesuper()in other methods, aside from constructors, in just the same way. This lets you extend or override the behavior of the superclass’s method.

TheSonclass inherits from theFatherand has itsspeakmethod. Thespeakmethod of theSonclass usessuper().speak()to call thespeakmethod of theFatherclass. This allows it to include the message from the parent class while extending it with a message specific to the child class.

the method resolution order

Failing to usesuper()in a method that overrides another means the functionality present in the parent class method won’t take effect. This results in a complete replacement of the method behavior, which can lead to behavior you didn’t intend.

Understanding Method Resolution Order

Method Resolution Order (MRO) is the order in which Python searches ancestorclasses when you access a method or an attribute. MRO helps Python determine which method to call when multiple inheritance hierarchies exist.

Here’s what happens when you create an instance of theLagosclass and call theculturemethod:

The output shows the Method Resolution Order ofLagos, starting from left to right.

Common Pitfalls and Best Practices

When working withsuper(), there are some common pitfalls to avoid.

Use super() the Right Way

Python’ssuper()function is a powerful feature when you’re working with inheritance and method overriding. Understanding howsuper()works and following best practices will let you create more maintainable and efficient code in your Python projects.