Calling __init__ with multiple inheritance

Peter Otten __peter__ at web.de
Tue Mar 29 03:37:06 EST 2005


Axel Straschil wrote:

> Thanks to all for the very interesting postings!

You're welcome.
 
> I came to the following:
> 
> For single inheritance, super is a nice tool if you will recfactoring
> the class later.

Or if you start out with a diamond inheritance graph from the beginning.
 
> For multiple inheritance, if you want to use super, you have to have
> very much knowledge of the classes you inheritance. For me, OOP is to
> not have to have the deep inner knowledge of the classes I inheritance
> from.

I failed to bring my point that you need _less_ knowledge across then.

> Also, for multiple inheritance, if think
> Mother1.__init__(self, ...)
> Mother2.__init__(self, ...)
> Mother3.__init__(self, ...)
> would be more clear to read then
> super(MyClass).__init__(Maby this will do some magic thing)

For an advantage to show up you need at least three levels of inheritance,
see my example with the Parent class.
 
> Also, I realy dislike
> __init__(self, param, **eat_some_needless_stuff)

In turn, I really like the name you gave the dictionary parameter :-)
Normalize the initializer signature to a common set of arguments. Or pass a
single parameter and take the actual information from its attributes. Or
write a custom dispatcher, that passes only parameters that correspond to
formal arguments...
 
> If I later extend my class and also add some parameters to __init__,
> what will happen to the classes using the baseclass?
> 
> Also, lool at that:
> class Mother(object):
> def __init__(self, param_mother='optional', **eat):
> print 'Mother'
> class Father(object):
> def __init__(self, param_father='optional', **eat):
> print 'Father'
> class Child(Mother, Father):
> def __init__(self, **ham):
> super(Child, self).__init__(**ham)
> child = Child(param_mother=1, param_father=1)
> 
> Father's init will not be called.

Change Father/Mother.__init__() to call the superclass initializer. It may
be counterintuitive, but it works.

Peter




More information about the Python-list mailing list