Calling __init__ with multiple inheritance

phil_nospam_schmidt at yahoo.com phil_nospam_schmidt at yahoo.com
Mon Mar 28 09:28:29 EST 2005


What if I want to call other methods as well? Modifying your example a
bit, I'd like the reset() method call of Child to invoke both the
Mother and Father reset() methods, without referencing them by name,
i.e., Mother.reset(self).

-------------------
class Mother(object):
    def __init__(self, p_mother, **more):
        print "Mother", p_mother, more
        super(Mother, self).__init__(p_mother=p_mother, **more)
    def reset(self):
        print 'resetting Mother'

class Father(object):
    def __init__(self, p_father, **more):
        print "Father", p_father, more
        super(Father, self).__init__(p_father=p_father, **more)
    def reset(self):
        print 'resetting Father'

class Child(Mother, Father):
    def __init__(self, p_mother, p_father, **more):
        print "Child", p_mother, p_father, more
        super(Child, self).__init__(p_mother=p_mother,
                                    p_father=p_father, **more)
    def reset(self):
        print 'resetting Child'

        # I would like to invoke both Mother.reset()
        # and Father.reset() here, but it doesn't work.
        print 'trying "super"'
        super(Child, self).reset()

        # These next two do work, but require referencing
        # Mother and Father directly.
        print "calling directly"
        Mother.reset(self)
        Father.reset(self)

a = Child(1, 2)
a.reset()
-------------------

Thanks,
Phil




More information about the Python-list mailing list