Cooperative methods, was Re: Calling __init__ with multiple inheritance

Peter Otten __peter__ at web.de
Mon Mar 28 10:10:08 EST 2005


phil_nospam_schmidt at yahoo.com wrote:

> 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).

[example snipped]

The problem with that aren't incompatible signatures, but the lack of an
implementation of the reset() method at the top of the diamond-shaped
inheritance graph that does _not_ call the superclass method. That method
could be basically a noop:


class Base(object):
    def reset(self):
        # object does something similar for __init__()
        pass
    
class Mother(Base):
    def reset(self):
        print "resetting Mother"
        super(Mother, self).reset()

class Father(Base):
    def reset(self):
        print "resetting Father"
        super(Father, self).reset()

class Child(Mother, Father):
    def reset(self):
        print "resetting Child"
        super(Child, self).reset()


Child().reset()

It might sometimes be convenient if such methods would magically spring into
existence for the object class, but for now you have to do it manually (or
perhaps someone has come up with a metaclass that I am not aware of).

Peter

PS: See http://www.python.org/2.2/descrintro.html for more on the subject.



More information about the Python-list mailing list