136

I'm begginer of python. I can't understand inheritance and __init__().

class Num:
    def __init__(self,num):
        self.n1 = num

class Num2(Num):
    def show(self):
        print self.n1

mynumber = Num2(8)
mynumber.show()

RESULT: 8

This is OK. But I replace Num2 with

class Num2(Num):
    def __init__(self,num):
        self.n2 = num*2
    def show(self):
        print self.n1,self.n2

RESULT: Error. Num2 has no attribute "n1".

In this case, how can Num2 access n1?

5 Answers 5

200

In the first situation, Num2 is extending the class Num and since you are not redefining the special method named __init__() in Num2, it gets inherited from Num.

When a class defines an __init__() method, class instantiation automatically invokes __init__() for the newly-created class instance.

In the second situation, since you are redefining __init__() in Num2 you need to explicitly call the one in the super class (Num) if you want to extend its behavior.

class Num2(Num):
    def __init__(self,num):
        Num.__init__(self,num)
        self.n2 = num*2
Sign up to request clarification or add additional context in comments.

6 Comments

Your citation isn't sufficient to explain why , when not defining a __init__ method in a derived-class, it gets inherited. It is because " if a requested attribute is not found in the class, the search proceeds to look in the base class." (doc)
I'm sorry... that's basicly how inheritance works... if you inherit a class, you get the whole package, so, everything in the superclass exists in the subclass. But, if you redefine a method, it gets overriden... that's what's up in your code.
@mario-duarte any reason why this would be better than super(Num2, self).__init__(num)?
I just switched from the solution proposed in this answer to using super, and my program now loads a few seconds faster. No idea why.
super is supposed to be helpful when using multiple inheritance. For single inheritance its benefits are not obvious.
|
48

When you override the init you have also to call the init of the parent class

super(Num2, self).__init__(num)

Understanding Python super() with __init__() methods

1 Comment

super(Num2,self).__init__(num)
29

A simple change in Num2 class like this:

super().__init__(num) 

It works in python3.

class Num:
        def __init__(self,num):
                self.n1 = num

class Num2(Num):
        def __init__(self,num):
                super().__init__(num)
                self.n2 = num*2
        def show(self):
                print (self.n1,self.n2)

mynumber = Num2(8)
mynumber.show()

1 Comment

This is why I love stackoverflow. Even though this isn't the answer to the question it's helpful. Sometimes the answers people post are the answers to the question that people should have been asking. Thank you!
4

Since you don't call Num.__init__ , the field "n1" never gets created. Call it and then it will be there.

Comments

1

Based on this: Can I collect and merge different answers into an accepted one?

I am creating a wiki answer collecting input from above two latest answers:
Answer with code by Sacchit Jaiswal & Tom Jelen
Answer with reason or why it's needed by Mauro Rocco


When you override the init you have also to call the init of the parent class
Understanding Python super() with __init__() methods

super(Num2, self).__init__(num)

It works in python3.

class Num:
        def __init__(self,num):
                self.n1 = num

class Num2(Num):
        def __init__(self,num):
                super().__init__(num)
                self.n2 = num*2
        def show(self):
                print (self.n1,self.n2)

mynumber = Num2(8)
mynumber.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.