super() doesn't get superclass

Hrvoje Niksic hniksic at xemacs.org
Tue Sep 18 11:41:46 EDT 2007


Bruno Desthuilliers <bruno.42.desthuilliers at wtf.websiteburo.oops.com> writes:

> If a class X is in the MRO of call Y, then X is a superclass of Y. I
> agree that the documentation for super is somewhat misleading (and
> obviously wrong), but it still *give access to* (at least one of)
> the superclass(es).

I believe the confusion comes from different assumptions about what
"superclasses" refers to.  super() iterates over superclasses of the
*instance* in use, but an individual call to super does not
necessarily invoke the superclass of the *implementation* of the
method.  For example, given a random class:

class X(Y):
  def foo(self):
    super(X, self).foo()

...there is in fact no guarantee that super() calls a superclass of
X.  However, it is certainly guaranteed that it will call a superclass
of type(self).

Pre-2.2 Python used a simpler scheme where the superclass was always
called, but it caused problems with diamond inheritance where some
methods would be called either twice or not at all.  (This is
explained in http://www.python.org/download/releases/2.2.3/descrintro/
in some detail.)



More information about the Python-list mailing list