The main difference is that if classes B and C have a common ancestor, with the super()
call, that ancestor's __init__()
will only be called once. By calling the parents' __init__()
method explicitly as in your example, the common ancestor's __init__()
will end up being called twice.
You can read about how super()
accomplishes this from Guido's explanation.
Below is a demonstration of the explicit call to the parents' __init__()
and how it calls the common ancestor twice:
>>> class A(object):
... def __init__(self):
... print "A"
...
>>> class B(A):
... def __init__(self):
... A.__init__(self)
... print "B"
...
>>> class C(A):
... def __init__(self):
... A.__init__(self)
... print "C"
...
>>> class D(B,C):
... def __init__(self):
... B.__init__(self)
... C.__init__(self)
... print "D"
...
>>> D()
A
B
A
C
D
<__main__.D object at 0x106de1d10>
>>>
And this is what happens with super()
>>> class A(object):
... def __init__(self):
... print "A"
...
>>> class B(A):
... def __init__(self):
... super(B,self).__init__()
... print "B"
...
>>> class C(A):
... def __init__(self):
... super(C,self).__init__()
... print "C"
...
>>> class D(B,C):
... def __init__(self):
... super(D,self).__init__()
... print "D"
...
>>> D()
A
C
B
D
<__main__.D object at 0x10d34ec90>
>>>