What are super()'s semantics?

Carl Banks pavlovevidence at gmail.com
Mon Sep 4 07:48:40 EDT 2006


Mike Krell wrote:
> class A(object):
>     def met(self):
>         print "A.met"
>
> class B(A):
>     def met(self):
>         print "B.met"
>         super(B, self).met()
>
> class C(A):
>     def met(self):
>         print "C.met"
>         super(C, self).met()
>
> class D(B,C):
>     def met(self):
>         print "D.met"
>         super(D, self).met()
>
> D().met() # essentially "D B C A"
[snip]

> 2. If I understand correctly, B's MRO is (B, A) and super(B, self) would
>    have an MRO of (A).

This is the source of your misunderstanding.

Essentially, it's objects that have MROs, not classes.  When you create
an object of class D, the MRO of that object is (D,B,C,A), and it
doesn't change, even when you're executing code defined in class B.
Thus, when self is of type D, super(B,self) does not have and MRO of
(A,), but (C,A).  Therefore, super(B,self).__init__() invokes
C.__init__.


Carl Banks




More information about the Python-list mailing list