1

Say I have the folowing code:

class Class1(object):

    def __init__(self):
        self.my_attr = 1
        self.my_other_attr = 2

class Class2(Class1):

    def __init__(self):
        super(Class1,self).__init__()

Why does Class2 not inherit the attributes of Class1?

4
  • You may also want to check out this article: fuhm.net/super-harmful Since I read that I would just do Class1.__init__(self) Commented Jun 16, 2010 at 13:23
  • @Wayne: That article is misleading and is somewhat frowned upon (but it is a very interesting and useful read). Multiple inheritance really is the problem, super is more like part of the solution. Picking on super is like saying that seatbelts are bad because you can still get hurt in an accident. You are generally far better of if you use super. Commented Jun 16, 2010 at 13:27
  • nikow, Are there any good reads that explain why super is better? Commented Jun 16, 2010 at 15:02
  • @Wayne: artima.com/weblogs/viewpost.jsp?thread=237121 is an excellent read, and explicitly mentions the "harmful" article. In MI situations using direct calling quickly becomes incredibly hard to do right, while super remains more manageable. However, its best to avoid MI altogether if possible. Commented Jun 16, 2010 at 15:14

2 Answers 2

10

You used super wrong, change it to

super(Class2, self).__init__()

Basically you tell super to look above the given class, so if you give Class1 then that __init__ method is never called.

Sign up to request clarification or add additional context in comments.

Comments

4

Because you're giving super the wrong class. It should be:

class Class2(Class1):

    def __init__(self):
        super(Class2,self).__init__()

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.