Tutorial 7 : OOPs in Python — super() function

Photo by Hitesh Choudhary on Unsplash

Missed previous tutorial ? : Click hare

We have already discussed about inheritance. In inheritance, super() function is used to access parent class methods directly inside child class without mentioning parent class name explicitly.

super() in single inheritance : Example 1

Example 2

Example 3

Example 4

super() in multiple inheritance : Example 5

Example 6

Example 7

Using super() function in child/derived class :

super().parent_class_method_name(parent_class_method_attributes)

Let’s see it using examples:

super() in single inheritance : Example 1

Here we have not created any object of parent class, still we can assign the values to the attributes of parent class using super() function.

Example 2 :

So we can directly assign the values to the attributes inside child class too without having to create parent class object.

Example 3 :

Example 4 :

super() in multiple inheritance : Example 5

Example 6 :

When two base classes have same method name with different functionalities then first base class is given preference. Let’s see it using example given below :

Example 7 :

When two base classes have different methods then we do not have to mention the base class from which method is going to be inherited when we use super() function. Let’s see it using example given below :

I hope now you have clear understanding of using super() function in single and multiple inheritance.

Congratulations !!

You have completed seventh tutorial. Take a deep breathe and move to the next tutorial.

--

--

--

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Making ethereum 2.0 executable beacon chain live

My Scrum’s not working… buuuhuuu!

2021 Here We Come — January 2021 Zillacracy Blog

Visual Studio Tips/Tricks/Shortcuts That I Use, and You Will Too

Screenshot of Visual Studio, displaying code and unit tests

Martin Fowler’s Design Pattern Transaction Script in C# (With UI)

Fight with `Flaky` tests or another side of using pytest-xdist

Software startup founder turned farmer

Top 10 Google Chrome Extensions for Front-End Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Patil

Gaurav Patil

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

More from Medium

Tutorial 5 : OOPs in Python — Encapsulation

Python Datatype : Dictionary

Python Unknown (Tips and Tricks) — Part 1

Top 5 List Operations in Python