[Python-Dev] Error in Python Tutorial on Multiple Inheritence

Steven Kah Hien Wong steven at zensaki.com
Thu Aug 12 03:20:20 CEST 2004


Hi,

From:

    http://docs.python.org/tut/node11.html#SECTION0011510000000000000000

This paragraph (last two sentences):

    It is clear that indiscriminate use of multiple inheritance is a
    maintenance nightmare, given the reliance in Python on conventions
    to avoid accidental name conflicts. A well-known problem with
    multiple inheritance is a class derived from two classes that happen
    to have a common base class. While it is easy enough to figure out
    what happens in this case (the instance will have a single copy of
    ``instance variables'' or data attributes used by the common base
    class), it is not clear that these semantics are in any way useful.

Maybe I have misread, but I have interpreted that as meaning the two
children will share the same instance of the base class. So if one child
changes its parent, the other child will pick up that change, too.

But I did some quick tests, and it turns out this isn't so.

    class CommonBase:
        x = 0

    class ChildOne(CommonBase):
        None

    class ChildTwo(CommonBase):
        None

    class ChildOneAndTwo(ChildOne, ChildTwo):
        def run(self):
            ChildOne.x = 1
            print "one =", ChildOne.x
            print "two =", ChildTwo.x

    ChildOneAndTwo().run()

And the output was:

    $ python multi.py
    one = 1
    two = 0

According to the documentation, I thought it should be:

    one = 1
    two = 1

Mind you, I find the current behaviour is much more useful, so I am
saying the documentation is in error, not the implementation. :) Have I
simply misread? If so, maybe that paragraph should be rewritten to make
it more clear. With an example like the above, perhaps?

Steven Wong


More information about the Python-Dev mailing list