Multiple inheritance in the Python and «diamond problem»

Published at 27, August 2008, author art. Tags: python

Hello, all!

Today I've write small and simple test for multiple inheritance in the python:

#!/usr/bin/env python
class Base(object):
    def say(self, indent = 0):
        print '%s%s' % ('\t'*indent, 'Base')
class ChildA(Base):
    def say(self, indent = 0):
        print '%s%s' % ('\t'*indent, 'ChildA')
        super(ChildA, self).say(indent+1)
class ChildB(Base):
    def say(self, indent = 0):
        print '%s%s' % ('\t'*indent, 'ChildB')
        super(ChildB, self).say(indent+1)
class ChildC(ChildA, ChildB):
    def say(self, indent = 0):
        print '%s%s' % ('\t'*indent, 'ChildC')
        super(ChildC, self).say(indent+1)
obj = ChildC()
obj.say()

Here is the output of the program:

ChildC
    ChildA
        ChildB
            Base

As you can see, methods are evaluated in right order!

See more information about multiple inheritance in the official Python Tutorial.

Comments

Subscribe on this post's comments

No comments. Wanna be first?

First, identify yourself and come back to leave comments.

If you wish to leave comment, please, identify yourself and then come back to this page.