Calling __init__ with multiple inheritance
Peter Otten
__peter__ at web.de
Mon Mar 28 09:08:05 EST 2005
Axel Straschil wrote:
> Im working with new (object) classes and normaly call init of ther
> motherclass with callin super(...), workes fine.
>
> No, I've got a case with multiple inherance and want to ask if this is
> the right and common case to call init:
>
> class Mother(object):
> def __init__(self, param_mother): print 'Mother'
> class Father(object):
> def __init__(self, param_father): print 'Father'
> class Child(Mother, Father):
> def __init__(self, param_mother, param_father):
> Mother.__init__(self, param_mother)
> Father.__init__(self, param_mother)
> child = Child(1, 2)
It looks correct -- at least it /looked/ correct before tried to quote it...
Here is an alternative approach that massages the initializer signatures a
bit to work with super() in a multiple-inheritance environment:
class Mother(object):
def __init__(self, p_mother, **more):
print "Mother", p_mother, more
super(Mother, self).__init__(p_mother=p_mother, **more)
class Father(object):
def __init__(self, p_father, **more):
print "Father", p_father, more
super(Father, self).__init__(p_father=p_father, **more)
class Child(Mother, Father):
def __init__(self, p_mother, p_father, **more):
print "Child", p_father, p_mother, more
super(Child, self).__init__(p_mother=p_mother, p_father=p_father,
**more)
Child(1, 2)
Unrecognized parameters are stashed away into the <more> dictionary.
Peter
More information about the Python-list
mailing list