Builtn super() function. How to use it with multiple inheritance? And why should I use it at all?

Ian Kelly ian.g.kelly at gmail.com
Fri Jul 30 23:40:21 EDT 2010


On Fri, Jul 30, 2010 at 6:38 AM, Hrvoje Niksic <hniksic at xemacs.org> wrote:
> Gregory Ewing <greg.ewing at canterbury.ac.nz> writes:
>
>> I think the point is that the name is misleading, because it makes it
>> *sound* like it's going to call a method in a superclass, when it fact
>> it might not.
>
> That is indeed confusing to some people, especially those who refuse to
> to accept the notion that "superclass" means the same as "next in MRO",
> maintaining instead that superclass refers to one of the base classes,
> their bases, etc. -- IMO a defensible position.
>
> super might have better been called next_in_mro, next_method, or
> next_class, except those are harder to type, and way less catchy than
> "super".  The Dylan and CLOS operator that super is most closely based
> on is called (call-)next-method.

I have to chime in and agree that the name "super" is problematic.
I'm reading this thread with a sense of alarm because I apparently
never read the super() documentation too closely (why would I?  "Oh,
it just accesses an attribute from a superclass.  Moving on.") and
have been writing code for the past four years under the impression
that super() will always refer to a superclass of the current class.
Fortunately, I don't use multiple inheritance often, and when I do I
prefer to write the superclasses explicitly (perhaps because of the
same misconception), so I probably haven't really abused it terribly.

On a tangent, is it just me, or is the super() documentation
incorrect, or at least unclear?  Quoting from the first two
paragraphs:

super(type[, object-or-type])

    Return a proxy object that delegates method calls to a parent or
sibling class of type. This is useful for accessing inherited methods
that have been overridden in a class. The search order is same as that
used by getattr() except that the type itself is skipped.

    The __mro__ attribute of the type lists the method resolution
search order used by both getattr() and super(). The attribute is
dynamic and can change whenever the inheritance hierarchy is updated.

In the first paragraph, "type" refers to "type", the first parameter
in the function signature.  In the second paragraph, "type" refers to
the type instance or the type of the object passed as the second
argument.  If it also referred to the first parameter, then super()
would always access a superclass as I initially thought; I wonder if
this might have been partially responsible for my confusion.

Cheers,
Ian



More information about the Python-list mailing list