Parent initializer not called in multi-level inheritence?

inheritance, initialization, python

Solution

You should be calling `super()` with the current class, not the parent.

class Base(object):
    def __init__(self):
        super(Base, self).__init__()
        print "BASE"

class Next(Base):
    def __init__(self):
        super(Next, self).__init__()
        print "NEXT"

class Final(Next):
    def __init__(self):
        super(Final, self).__init__()    
        print "FINAL"

f = Final()

At first glance this might seem redundant ("why can't it just get the class from `self`?") - but keep in mind that the same `self` is passed to all three of these `__init__` methods when `f` is created, and that `self` is always of class `Final`. Thus, you have to pass `super()` the class that you want it to find the parent of.

Problem

I have a class scheme with 2-levels of inheritance. My expectation is that each class constructor would run through- and yet the mid-level class constructor never seems to get hit. What's missing here? ``` class Base(object): def __init__(self): print "BASE" class Next(Base): def __init__(self): super(Base, self).__init__() print "NEXT" class Final(Next): def __init__(self): super(Next, self).__init__() print "FINAL" f = Final() ``` Outputs: BASE FINAL Why does "NEXT" not print??

Original source