Search code examples
pythonsuper

what is the Use of Super() in child class even we are not passing any argument


    class Base(object):
        def __init__(self):
           self.fname="MS"
           self.lname="Dhoni"
    
    class Child(Base):
       def __init__(self):
           self.fname="kohli"
           super(Base).__init__()

What is use of super method in above code even commenting the super(Base).__init__() I am getting output kohli please explain


Solution

  • You're calling super(Base) which means the parent of Base class who is object class, so you're not calling the Base.__init__ method, which means no re-assignment of fname which stays to kohli


    What you want is parent of Child class, with current instance self

    super(Child, self).__init__()
    

    But in fact you can just do the following, that's the same

    super().__init__()