Why does calling Parent classes with super() does not work while using 'direct' call works fine?
class A(object):
def __init__(self, x):
self.x = x
print("Inside A __init__. x = %s" % self.x)
class B(object):
def __init__(self, y):
self.y = y
print("Inside B __init__. y = %s" % self.y)
class C(A,B):
def __init__(self, z):
super(C, self).__init__(6)
super(C, self).__init__(5)
#1. A.__init__(self,6)
#2. B.__init__(self,5)
self.z = z
print("Inside C __init__. z = %s" % self.z)
if __name__ == "__main__":
log = C(2)
With uncommented 'super' the result I am getting is:
Inside A __init__. x = 6
Inside A __init__. x = 5
Inside C __init__. z = 2
so the code for 'B' class init is never called. But after using the commented lines '#1', and '#2' the code works as it should:
Inside A __init__. x = 6
Inside B __init__. y = 5
Inside C __init__. z = 2
Questions:
- What is the cause of this strange 'super()' behaviour.
- Can super() call the init in 'B'?
- Is there any other way of calling all 'init's from Parent classes?
superis not a drop-in replacement for accessing a specific parent class. Its use must be coordinated among all the classes involved.