super, apply, or __init__ when subclassing?

Duncan Booth duncan.booth at invalid.invalid
Tue Sep 18 05:15:48 EDT 2007


"exhuma.twn" <exhuma at gmail.com> wrote:

> So, knowing that in python there is one thing to do something, these
> three different calls must *do* domething different. But what exactly
> *is* the difference?
> 
> ------------ Exampel 1: -----------------------------
> 
> class B(A):
>    def __init__(self, *args):
>       A.__init__(self, args)
> 
> ------------ Exampel 2: -----------------------------
> 
> class B(A):
>    def __init__(self, *args):
>       apply( A.__init__, (self,) + args)
> 
> ------------ Exampel 3: -----------------------------
> 
> class B(A):
>    def __init__(self, *args):
>       super(A,self).__init__(*args)

Yes, they are all different.

The first one calls B's immediate base class but packs all of the 
arguments together into a single tuple. Probably not what you meant.

The second one passes B's positional arguments to its immediate base 
class without messing them up but uses a deprecated function to do it. 
You should use "A.__init__(self, *args)" instead unless you are 
concerned about multiple inheritance.

The third one skips over the __init__ method in the immediate base class 
and calls the __init__ method in whatever class follows A in the MRO 
instead. Probably not what you meant either.



More information about the Python-list mailing list