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() # has four legs
In above code by using super() we can access the methods of the base class animals(). While not using super() we can access the methods of parent class animals() by using the line animals.__init__(self,name,5)
.
class animals():
home='Jungle' #class attribute
#instance attributes
def __init__(self):
self.name= 'Alex'
self.height=2
def legs(self):
print("has four legs")
class carnivores(animals):
def __init__(self):
self.food='flseh'
super().__init__()
class wild(carnivores):
def __init__(self):
self.weight=5
#animals.__init__(self,name,5)
super().__init__()
tiger=wild() # object is created.
print(tiger.weight) # 3
print(tiger.food) # flesh
print(tiger.name) # Alex
print(tiger.height) # 2
tiger.legs() # has four legs
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.