super, apply, or __init__ when subclassing?

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Sep 18 04:19:41 EDT 2007


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

> This is something that keeps confusing me. If you read examples of
> code on the web, you keep on seeing these three calls (super, apply
> and __init__) to reference the super-class. This looks to me as it is
> somehow personal preference. But this would conflict with the "There
> one way to do it" mind-set.
> 
> 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)

Note that your examples 1 and 3 aren't different *calls*. They are
different ways of *getting at* the same class: either name it
explicitly (as in example 1) or use 'super' to get it for you.

Also, example 3 should instead call 'super(B, self).__init__'. If
you're going to go to the bother of actually *specifying* the class
'A', it's silly to call 'super' to get at it *again*.


The broader picture: I think you're right that this is ridiculously
difficult in Python. The community elders would have us use 'super' to
get at our inherited '__init__', but that doesn't work very well
<URL:http://fuhm.org/super-harmful/> so most people use your example
1.

-- 
 \        "The most merciful thing in the world... is the inability of |
  `\         the human mind to correlate all its contents."  -- Howard |
_o__)                                                Philips Lovecraft |
Ben Finney



More information about the Python-list mailing list