Accessing derived class vars
#1
i wouldnt normally bother the forum with python coding problems but my r&d for this has drawn a blank, so if you know something about inheritance from derived class, please consider this queston:

Quote:class myclassa:
def init(self):
self.x = 10 # baseclass var i want to access

def func1(self):
print 'hello'

def ask(self):
b = myclassb() # instantiates myclassb

class myclassb(myclassa): # derived from baseclass myclassa
def init(self):
print x # this doesn't see x
print self.x # neither does this
print myclassa.x # neither does this

print self.func1() # inherited function, it works

so my questiion is;
how can i get myclassb to 'see' the var x defined in myclassa?
as i can call functions defined in myclassa from myclassb (die to inheritance), i was hoping i could see its vars too, but thats not the case.


the only way i can get it to work is to also pass 'self', like this:

Quote:class myclassa:
def init(self):
self.x = 10

def func1(self):
print 'hello'

def ask(self):
b = myclassb() # instantiates myclassb

class myclassb(myclassa):
def init(self, motherclass):
print self.motherclass.x # now it works

print self.func1() # inherited function, it works

this also works, callng the 'init' of myclassa;
Quote:class myclassa:
def init(self):
self.x = 10

def func1(self):
print 'hello'

def ask(self):
b = myclassb()

class myclassb(myclassa):
def init(self):
myclassa.(self)
print self.motherclass.x # also, now it works

print self.func1() # inherited function, it works

so, although i've got it working, which is that the correct way to do it 1) to pass self or 2) to call myclass.(self) or 3) someotherway?

any help appreciated.
thanks
bbb

*nb* forum seems to remove double underscroes that should preceed and postfix 'init'
Reply

Logout Mark Read Team Forum Stats Members Help
Accessing derived class vars0