super()

super() returns access the base class methods

By using super() we need not use the name of the base class explicitly.
It is useful in multiple inheritance
Read more on inheritance

Example with inheritance

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).

By using super() we can avoid explicit use of base class name. The advantage is while using multilevel inheritance.

Multiple inheritance

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


All Built in Functions in Python locals() dir() ord()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer