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 & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.