class Child(SomeBaseClass):
def __init__(self):
SomeBaseClass.__init__(self)
This is fairly easy to understand.
class Child(SomeBaseClass):
def __init__(self):
super(Child, self).__init__()
Ok, what happens now if you use super(Child,self)
?
When a Child instance is created, its MRO(Method Resolution Order) is in the order of (Child, SomeBaseClass, object) based on the inheritance. (assume SomeBaseClass doesn't have other parents except for the default object)
By passing Child, self
, super
searches in the MRO of the self
instance, and return the proxy object next of Child, in this case it's SomeBaseClass, this object then invokes the __init__
method of SomeBaseClass. In other word, if it's super(SomeBaseClass,self)
, the proxy object that super
returns would be object
For multi inheritance, the MRO could contain many classes, so basically super
lets you decide where you want to start searching in the MRO.