Multiple inheritance with a common base class

Markus Bertheau twanger at bluetwanger.de
Tue Aug 10 14:19:05 CEST 2004


В Втр, 10.08.2004, в 13:37, Markus Bertheau пишет:

Also I observe that the instance will in fact _not_ have a single copy
of the data attributes used by the common base class. The following
example demonstrates this:

class CommonBase:
    def __init__(self):
        self.no = 0
    def setNo(self, no):
        self.no = no
 
class LeafA(CommonBase):
    def __init__(self):
        CommonBase.__init__(self)
        print("CommonBase.no: %i" % self.no)
        CommonBase.setNo(self, 3)
 
class LeafB(CommonBase):
    def __init__(self):
        CommonBase.__init__(self)
        print("CommonBase.no: %i" % self.no)
        CommonBase.setNo(self, 4)
 
class Multi(LeafA, LeafB):
    def __init__(self):
        LeafA.__init__(self)
        LeafB.__init__(self)
 
m = Multi()

It outputs:

CommonBase.no: 0
CommonBase.no: 0

If there was only one copy of the common base class, I'd have expected
an output similar to

CommonBase.no: 0
CommonBase.no: 3

This renders multiple inheritance pretty useless for me.

Can someone clear this all up and tell me how multiple inheritance is
supposed to work in python?

Thanks

-- 
Markus Bertheau <twanger at bluetwanger.de>




More information about the Python-list mailing list