Newby Q: nested classes, access of upper method

G

Gregor Horvath

Hello,

class A(self):
def A1():
pass

class B(self):
def B1():
#************************************
#*** How can I access A1 here???? ***
#************************************
self.A1() # doesnet work because self references to B
self.self.A1() #doesnt work either


Renanimg class B(self1): doesnt work either because self is not bound.

How can I access a method of a "upper" class?
 
N

Nick Coghlan

Gregor said:
Hello,

class A(self):
def A1():
pass

class B(self):
def B1():
#************************************
#*** How can I access A1 here???? ***
#************************************
self.A1() # doesnet work because self references to B
self.self.A1() #doesnt work either


Renanimg class B(self1): doesnt work either because self is not bound.

OK, I suspect you're a little confused about how classes work. The items in
brackets after a class name are the *base* classes of a class, not the way the
class refers to itself. So Python will complain if the listed items can't be
inherited from for one reason or another.

I suggest having another read of the tutorial section on classes to figure out
exactly what you want to be doing:
http://www.python.org/doc/2.3.4/tut/node11.html
How can I access a method of a "upper" class?

Merely defining one class inside another class does not automatically give
instances of that inner class a reference to an instance of the outer class - if
such a reference is needed, it must be provided in the inner class's constructor.

E.g.

class A(object):
class B(object):
def __init__(self, owner):
self._owner = owner

def B1(self):
self._owner.A1()

def A1(self):
pass

def makeB(self):
return A.B(self)

Cheers,
Nick.
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top