| Store | Cart

Calling __init__ with multiple inheritance

From: Peter Otten <__pe...@web.de>
Tue, 29 Mar 2005 10:37:06 +0200
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

Recent Messages in this Thread
Axel Straschil Mar 28, 2005 12:51 pm
Peter Otten Mar 28, 2005 02:08 pm
jfj Mar 29, 2005 03:27 am
Peter Otten Mar 28, 2005 05:51 pm
jfj Mar 29, 2005 04:53 am
Peter Otten Mar 28, 2005 07:53 pm
Axel Straschil Mar 29, 2005 05:53 am
Steven Bethard Mar 29, 2005 07:02 am
Peter Otten Mar 29, 2005 08:37 am
Axel Straschil Mar 29, 2005 12:21 pm
jfj Mar 29, 2005 07:38 pm
Robert Dick Mar 28, 2005 07:14 pm
phil...@yahoo.com Mar 28, 2005 04:07 pm
Messages in this thread