How to use the super() function in Python

Issue related to: Core Python – OOP

Issue Link: https://www.reddit.com/r/learnpython/comments/g2wbqp/function_super/

Issue:

Hello everyone!!

I’m just learning OOP with Python Crash Course and resources. I’m not sure what function super() does, i cannot see the difference between using it or not. For example:

First code:

class Car:
 def __init__(self, make, model, year):
 self.make = make
 self.model = model
 self.year = year
class ElectricCar(Car):
 def __init__(self, make, model, year):
 super().__init__(make, model, year)
 def display_electric_car(self):
 print(f"Description of the car: {self.make}, {self.model}, {self.year} ")

Second code:

class Car:
 def __init__(self, make, model, year):
 self.make = make
 self.model = model
 self.year = year
class ElectricCar(Car):
 def display_electric_car(self):
 print(f"Description of the car: {self.make}, {self.model}, {self.year} ")

The latter doesn’t use super() function and works perfect.

Explanation 

The super() function allows the child class to access the methods of the parent class as well as altering the parents methods for its own use. 

In our case

the parent class is class Car 

the child class is class ElectricCar

Now we will begin to create our class Car – which will have the attributes make,model and year.

class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

Our car class will have a also function showInfo which will show the details of the car 

 def showInfo(self):
print(f"Description of the car: {self.make}, {self.model}, {self.year} ")

In summay our car has two functions – initalise and showInfo

class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

def showInfo(self):
print(f”Description of the car: {self.make}, {self.model}, {self.year} “)

Our electric car will have the attributes make,model,year and power level.

 

As make,model and year the attributes in car are also in electric car we will pass car into the electric car class 

class ElectricCar(Car):
pass

Now our electric car class has two functions; initalise and showInfo

We can prove this by creating a tesla:

tesla = ElectricCar("Tesla","Model 3",2020)

Now we want to use the function showInfo():

tesla.showInfo()

This will return the following output thus proving our point that the functions; initalise and showInfo are present in class ElectricCar despite no functions physically in class ElectricCar:

Description of the car: Tesla, Model 3, 2020

 

Now we want to add the power level attribute to our class ElectricCar We could write:

class ElectricCar(Car):
    def __init__(self, make, model, year,power):
        self.make = make
        self.model = model
        self.year = year
        self.power = power

Instead we will use the super function:

class ElectricCar(Car):
    def __init__(self, make, model, year,power):
        super().__init__(make, model, year)
        self.power = power

This takes the parameters from the previous class and adds an extra parameter. 

In addition we will adjust the function showInfo in class ElectricCar to demonstrate that this is working.

class ElectricCar(Car):
    def __init__(self, make, model, year,power):
        super().__init__(make, model, year)
        self.power = power
    def showInfo(self):
        print(f"Description of the car: {self.make}, {self.model}, {self.year} and {self.power}kwh ")

 We can now test this function:

tesla = ElectricCar("Tesla","Model 3",2020,50)
tesla.showInfo()

which outputs:

Description of the car: Tesla, Model 3, 2020 and 50kwh