473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Newby Q: nested classes, access of upper method

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?

--
Greg
Jul 18 '05 #1
2 1645
Gregor Horvath wrote:
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.
Jul 18 '05 #2
Hello Nick,

thank you, your answer really helped me..

--
Greg
Nick Coghlan wrote:
Gregor Horvath wrote:
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.

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert M. Gary | last post by:
I'm curious what the ANSI C++ standard says about nested classes. I'm not able to find where in the ANSI C++ standard this is addressed. The issue is the accessibility of sibling nested classes....
6
by: A | last post by:
Hi, How do you make use of nested functions in C++? I realize in C++ that everything must be declared first in a header file before implementation in a .cpp file. I tried to nest a method...
3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
5
by: ZikO | last post by:
Hi there. I have a problem. I have created nested classes but don't know how to access to inner classes. I know I can create objects: Hen Object; Hen::Nest ObjectNest; Hen::Nest::Egg...
5
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.