Passing Arguments in Inheritance in Python

Passing arguments to function of class in inheritance in Python programming. Inheritance allows us to inherit attributes and methods from one class to another class.

Inheritance Passing Arguments and using Default Constructor

Program

class cls: #'cls' is name of class.
    def fun(self):
        print("This is Function of class cls")
    def fun2(self, arg):
        print("This is "+ arg +" Function of class cls")
class cls2(cls): #class 'cls2' is Child of Class 'cls'.
    def fun3(self):
        print("This is Simple Function of class cls2")
    def fun4(self, arg2):
        print("This is "+ arg2 +" Function of Class cls2")
class cls3(cls2): #class 'cls3' is Child of Class 'cls2'.
    def __init__(self):
        print("This is Default Constructor of class cls3")
    def fun5(self, arg3):
        print("This is "+ arg3 +" Function of Class cls3")
ob = cls3() #'ob' is object of class 'cls3', and calling Constructor.
ob.fun3()
ob.fun4("Parameterized")
ob.fun()
ob.fun2("Parameterized")
ob.fun5("Parameterized")
Output
This is Default Constructor of class cls3
This is Simple Function of class cls2
This is Parameterized Function of Class cls2
This is Function of class cls
This is Parameterized Function of class cls
This is Parameterized Function of Class cls3

Program Inheriting Multiple Classes from One Class

Program

class cls: #'cls' is name of class.
    def fun(self):
        print("This is Function of class cls")
    def fun2(self, name):
        print("My Name is: ", name)
class cls2(cls): #class 'cls2' is Child of class 'cls'.
    def __init__(self):
        print("I am Default Constructor of class cls2")
class cls3(cls): #class 'cls3' is Child class of class 'cls'.
    def __init__(self):
        print("I am Default Constructor of class cls3")
ob = cls3() #'ob' is object of class 'cls3'.
ob.fun()
ob.fun2("HiLaLs Afridi")
ob2 = cls2() #'ob2' is object of class 'cls2'.
ob2.fun()
ob2.fun2("Bilal Khan")
Output
I am Default Constructor of class cls3
This is Function of class cls
My Name is: HiLaLs Afridi
I am Default Constructor of class cls2
This is Function of class cls
My Name is: Bilal Khan