In the below sample, the last 2 lines in the B.Go() method both call the Go() method from class A. Are they functionally identical? Is the only benefit to using super() that I don’t have to know the inherited class name?
class A(object):
def Go(self):
print "Calling A.Go()"
class B(A):
def Go(self):
super(B, self).Go()
A.Go(self)
inst = B()
inst.Go()
No,
super()does something a direct call toA.Gocan’t.super(B, self).Go()calls the next method in the method resolution order. It calls the method that would have been called hadBnot implemented the method at all. In direct linear inheritance this is alwaysA, so there is no real difference (besides the class you repeat.) In the case of multiple inheritance, however, the next class in the normal method resolution order is not necessarily the direct baseclass of the current class. See the explanation for diamond inheritance in Guido’s original explanation of these features.