super question

A. Lloyd Flanagan alloydflanagan at attbi.com
Tue Apr 8 16:03:33 CEST 2003


Lee Harr <missive at frontiernet.net> wrote in message news:<Qdpka.2889$Zu2.2627 at news01.roc.ny.frontiernet.net>...
...
> 
> From the super docs ;o)  it says you might do this:
> 
>     class C(B):
>         def meth(self, arg):
>             super(C, self).meth(arg)
> 
> 
> but why not just do:
> 
>     class C(B):
>         def meth(self, arg):
>             B.meth(self, arg)
> 
> 
> 
> Is there any difference?
> 
> Any advantage either way?

If all your classes only have one parent, it doesn't make a
difference.  If your classes a) use multiple inheritance, b) several
of them define a method with the same name, and c) all of them call
super() to execute that method, then python does some really neat
stuff to ensure that all the methods of that name get called, and that
it occurs in the "correct" order.

So super() is preferable if a) you're redefining a method but want to
call the base class method(s), and b) your class is part of a multiple
inheritance graph OR may be used in a multiple inheritance graph.

Disclaimer:  I'm new to this stuff myself, so if somebody's got a
better explanation, please speak up!




More information about the Python-list mailing list