Python | super () function with multilevel inheritance

| | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

To understand Python’s superfunction, we need to know about

# the parent class is also sometimes called the superclass

class Parentclass ():

def __ init __ ( self ):

pass


# derived or subclass
# initialize parent or base class in subclass

class subclass (Parentclass):

def __ init __ ( self ):

# calling the super () function to simplify the process

super ()

Python super () function with multilevel inheritance.

Since we learned that the Python super () function allows us to implicitly refer to a superclass. But in layered inheritance, the question is that there are so many classes, so which class does the super () function refer to?
Well, the super () function has a property that always refers to the immediate superclass . In addition, the super () function not only references __init __ (), but can also call other superclass functions when needed.

Here example explaining multiple inheritance.

# Program for detecting super () usage
# function in multiple inheritance

class GFG1:

def __ init __ ( self ):

print ( ’HEY !!!!!! GfG I am initialised (Class GEG1)’ )

def sub_GFG ( self , b):

print ( ’Printing from class GFG1:’ , b)


# GFG2 class inherits from GFG1

class GFG2 (GFG1):

def __ init __ ( self ):

print ( ’HEY !!!!!! GfG I am initialised (Class GEG2) ’ )

super () .__ init __ ()

def sub_GFG ( self , b):

print ( ’Printing from class GFG2:’ , b)

super (). sub_GFG (b + 1 )


# GFG3 class inherits GFG1 and GFG2 both

class GFG3 (GFG2):

def __ init __ ( self ):

print ( ’ HEY !!!!!! GfG I am initialised (Class GEG3) ’ )

super () .__ init __ ()

def sub_GFG ( self , b):

print ( ’Printing from class GFG3:’ , b)

super (). sub_GFG (b + 1 )


# main function

if __ name__ = = ’__main__’ :

# created gfg object

gfg = GFG3 ()

# call the sub_GFG3 () function from the GHG3 class

# which inherits the GFG1 and GFG2 classes

gfg.sub_GFG ( 10 )

Exit:

 HEY !! !!!! GfG I am initialised (Class GEG3) HEY !!!!!! GfG I am initialised (Class GEG2) HEY !!!!!! GfG I am initialised (Class GEG1) Printing from class GFG3: 10 Printing from class GFG @: 11 Printing from class GFG1: 12 

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Python | super () function with multilevel inheritance, check other __main__ Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Anna Schteiner

Prague | 2023-02-07

Thanks for explaining! I was stuck with Python | super () function with multilevel inheritance for some hours, finally got it done 🤗. I am just not quite sure it is the best method

Carlo Gonzalez

Munchen | 2023-02-07

Thanks for explaining! I was stuck with Python | super () function with multilevel inheritance for some hours, finally got it done 🤗. Will use it in my bachelor thesis

Olivia Jackson

Berlin | 2023-02-07

I was preparing for my coding interview, thanks for clarifying this - Python | super () function with multilevel inheritance in Python is not the simplest one. I am just not quite sure it is the best method

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically