Jean-Michel Pichavant <jeanmic...@sequans.com> writes: [...] > Then is there a reason why >>>> return super(Subclass, self).parrot() > would be prefered over the "classic" >>>> return Base.parrot(self) > ? > > Or is it just a matter of preference ?
Using super() calls the next method in the class's Method Resolution Order (mro - to see it on class A, use A.mro()). This guarantees that ancestor methods won't be called twice when the inheritance graph is not a tree. Here is a (minimal?) example. >>> class A(object): ... def foo(self): print 'A' ... >>> class B(A): ... def foo(self): ... super(B, self).foo() ... print 'B' ... >>> class C(A): ... def foo(self): ... super(C, self).foo() ... print 'C' ... >>> class D(B, C): ... def foo(self): ... super(D, self).foo() ... print 'D' ... >>> d = D() >>> d.foo() A C B D >>> D.mro() [<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <type 'object'>] The reason why super() may be necessary is that if an object is an instance of say class C, its method resolution order above class C is not known at compile time. HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list