inheritance with new-style classes - help

G

Greg Copeland

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

Mother
Father
Base
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...
{'_attrib': 'base'}

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

Can someone please shine some light here? Please?


Thanks in advance,

Greg
 
G

Greg Copeland

Doh! Child's __init__ was declared as __init(). Fixing that took care
of it! Sorry for wasting the bandwidth!

Cheers,

Greg
 
T

Terry Reedy

Greg Copeland said:
Okay, I have: [snip]
self._childAttrib = "child" ....
AttributeError: 'Child' object has no attribute '_childAttrib'{'_attrib': 'base'}

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

I don't immediately see the problem. Failing that, I would rerun with
print 'setting '_childAttrib' # and
print self._childAttrib
bracketing the line where it should be set, and go one from there.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top