inheritance with new-style classes - help

Greg Copeland gtcopeland2002 at yahoo.com
Fri May 6 13:58:16 EDT 2005


Okay, I have:
class Base( object ):
    def __init__( self ):
        self._attrib = "base"
        print "Base"


    def real( self ):
        print "Base.real() is calling base.virtual()"
        self.virtual()


    def virtual( self ):
        print "Base virtual()"
        pass



class Mother( Base ):
    def __init__( self ):
        print "Mother"
        super( Mother, self ).__init__()


    def virtual( self ):
        print self._attrib
        print "virtual = Mother"


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

    def virtual( self ):
        print self._attrib
        print "virtual = Father"



class Child( Mother, Father ):
    def __init( self ):
        print "Child"
        super( Child, self ).__init__()

        self._childAttrib = "child"


    def virtual( self ):
        print "base attribute = " + self._attrib
        print "virtual = Child"
        print "childAttrib = " + self._childAttrib



rename = Child


>>> x = rename()
Mother
Father
Base
>>> x.virtual()
base attribute = base
virtual = Child
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/tmp/python-8zAJdg.py", line 51, in virtual
AttributeError: 'Child' object has no attribute '_childAttrib'

Hmmm...interesting....but okay...let's look some more...

>>> x.__dict__
{'_attrib': 'base'}

What??!  Where the heck did self._childAttrib go?  And why?

Can someone please shine some light here?  Please?


Thanks in advance,

Greg




More information about the Python-list mailing list