0
    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

dejanualex
  • 3,872
  • 6
  • 22
  • 37
Wanted123
  • 23
  • 7
  • It calls the `__init__()` method of `object`. – CivFan Jan 21 '21 at 17:42
  • Does this answer your question? [Understanding Python super() with \_\_init\_\_() methods](https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods) – Tomerikoo Jan 21 '21 at 17:44

1 Answers1

6

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__()
azro
  • 53,056
  • 7
  • 34
  • 70
  • but why i am getting output kohli that should be MS ?please explain what would be the output in both case when "super keyword" is commented and uncommented ? – Wanted123 Jan 21 '21 at 17:45
  • @Wanted123 please read again the first paragraph of my answer ;) – azro Jan 21 '21 at 17:46