class x(): # base or parent class
def x_one(self):
print("I am x_one")
class y(x):# derived or child class
def y_one(self):
print("I am y_one")
y1=y() # object declaration of class y()
y1.x_one()
Output
I am x_one
class x():
def x_one(self):
print("I am x_one")
class y(x):
def y_one(self):
print("I am y_one")
class z(y):
def z_one(self):
print("I am z_one")
z1=z() # object of z class
z1.x_one()
z1.y_one()
z1.z_one()
Output
I am x_one
I am y_one
I am z_one
class x():
def x_one(self):
print("I am x_one")
class y():
def y_one(self):
print("I am y_one")
class z(x,y): # z is child of x and y
def z_one(self):
print("I am z_one")
z1=z()
z1.x_one()
z1.y_one()
z1.z_one()
Output
I am x_one
I am y_one
I am z_one
class x(): # base or parent class
def m1(self):
print("I am x m1")
class y(x):# derived or child class
def m1(self):
print("I am y m1") # this will execute
y1=y()
y1.m1()
Output
I am y m1
class x(): # base or parent class
def m1(self):
print("I am x m1")
class y():
def m1(self):
print("I am y m1")
class z(x,y): # multiple inheritance
pass
z1=z() # object of z()
z1.m1() # method with class x() will be executed
Output
I am x m1
In above code change the class declaration of z like this.
class z(y,x): # multiple inheritance
Now check the output
I am y m1
We can use mro() method to check the mro of the class.
print(z.mro())
Output ( we will get a list )
[<class '__main__.z'>, <class '__main__.y'>, <class '__main__.x'>, <class 'object'>]
class animals():
home='Jungle' #class attribute
#instance attributes
def __init__(self,name,height):
self.name= name
self.height=height
class wild(animals):
weight=2
tiger=wild('Tom',3) # object is created.
print(tiger.name, tiger.height,tiger.weight,tiger.home,sep=',')
Output
Tom,3,2,Jungle
lion=animals('Alex',5) # object
print(lion.name) # Alex
print(lion.weight) # error
The last line will generate error as weight is not an attribute of base class animal() ( it is attribute of child class wild() )
print(tiger.height)
in below code will generate error as we have not inherited the base __init__() method.
class animals():
#instance attributes
def __init__(self,name,height):
self.name= name
self.height=height
def legs(self):
print("has four legs")
class wild(animals):
def __init__(self,name,weight):
self.name=name
self.weight=weight
tiger=wild('Tom',3) # object is created.
print(tiger.weight)
print(tiger.height) # Error
To use the base class methods we will add this line animals.__init__(self,name,5)
to our __init__() in child class.
class animals():
home='Jungle' #class attribute
#instance attributes
def __init__(self,name,height):
self.name= name
self.height=height
def legs(self):
print("has four legs")
class wild(animals):
def __init__(self,name,weight):
self.name=name
self.weight=weight
animals.__init__(self,name,5)
tiger=wild('Tom',3) # object is created.
print(tiger.weight) # 3
print(tiger.height) # 5
animals.__init__(self,name,5)
We need not explicitly declare the base class and just use super() function in python to use all the methods of base class. This is a big advantage while using multiple inheritance. Here is the example of linking base class by using super().
super().__init__(name,5)
The full code is here
class animals():
home='Jungle' #class attribute
#instance attributes
def __init__(self,name,height):
self.name= name
self.height=height
def legs(self):
print("has four legs")
class wild(animals):
def __init__(self,name,weight):
self.name=name
self.weight=weight
#animals.__init__(self,name,5)
super().__init__(name,5)
tiger=wild('Tom',3) # object is created.
print(tiger.weight) # 3
print(tiger.height) # 5
tiger.legs()
print(isinstance(tiger,animals)) # True
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.