473,670 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1660
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
3651
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. Example... class A{ private: class B{ }; class C{
6
12195
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 prototype in another prototype but seems pointless. Can someone please write a short, simple, and concise skeleton code of how to use nested functions? class Foo {
3
2398
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
3941
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 private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
6
559
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 like MyClass.MyColors = Color.Green or, a 'get', such as Color forecolor = MyClass.MyColors; I want to use an indexer so I can take parameters, such as the color type (e.g. "Foreground", "Background" etc.). With a single member function I couldn't...
8
16890
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. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
2
2367
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 when a nested class inherits from it’s parent class you get this infinite class chain in intellisense when consuming the class. To get around this I created two child classes Reader and Writer which require a base Person object. When consuming...
5
2188
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 ObjectEgg; and have access to particular elements
5
2280
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
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8661
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2802
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.