super(...).__init__() vs Base.__init__(self)

Steven Bethard steven.bethard at gmail.com
Thu Feb 9 21:44:16 EST 2006


Jan Niklas Fingerle wrote:
> Steven Bethard <steven.bethard at gmail.com> wrote:
>> Personally, I'd call the lack of the super calls in threading.Thread and 
>> Base bugs.  
> 
> It can't be a bug since it wasn't a bug before super was introduced and
> you don't wan't to break working Python-2.x-code. 

Just because there wasn't a bugfix available at the time doesn't mean it 
wasn't a bug. ;) The threading.Thread class does not properly call 
sibling constructors in multiple inheritance. This should either be 
fixed in the implementation (by introducing a call to super) or fixed in 
the documentation (by indicating that threading.Thread does not support 
multiple inheritance in its __init__() method).

>> But __init__() is definitely a tricky case since the 
>> number of arguments tends to change in the __init__() methods of classes...
> 
> ACK. And every __init__ will have to accept *any* arguments you give to
> it and call super with *all* the arguments it got. This is tricky and
> easily to get wrong. Super is a good tool to use, when dealing with
> diamond shape inheritance. In any other case I would use the direct
> calls to the base classes. In fact, i've yet to find a non-textbook-case
> where I really need diamond shape inheritance. OTOH I don't mean to say
> that noone else needs it either.

I've used diamond inheritance exactly once, and all the classes under 
that hierarchy were under my control, so they all used super properly. 
And fortunately, the constructors of those classes didn't take any 
arguments, so I didn't run into any of the nastier sides of super.

Using super is guaranteed to work as long as the number of arguments of 
the method does not change from that of the superclass.  For the 
__init__() method, this means that super is guaranteed to work as long 
as it takes no arguments, since object.__init__() takes no arguments. 
Sure, I'd love to see super work right in other cases, but for the OP's 
situation at least, super already does what it's supposed to.

STeVe



More information about the Python-list mailing list