473,602 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to access outer class attribute from nested class

Please see the following example. Is there any other way to access
SomeAttribute from the nested class, without having to use the actual
name of the outer class?
class SomeClass:
SomeAttribute = 1

class SomeNestedClass :
SomeOtherAttrib ute = 2

def SomeNestedClass Method(cls):
print "%s%s" % (
SomeClass.SomeA ttribute
cls.SomeOtherAt tribute,
)
SomeNestedClass Method = classmethod(Som eNestedClassMet hod)

Thanx
Jul 18 '05 #1
2 9705
I use something like:

class SomeClass:
SomeAttribute = 1

class SomeNestedClass :
SomeOtherAttrib ute = 2
def __init__(self, parent):
self.parent=par ent
return

def SomeNestedClass Method(self):
print "%s-%s" % (
self.parent.Som eAttribute,
self.SomeOtherA ttribute)

def __init__(self):
self.SNC=self.S omeNestedClass( self)
return

if __name__=="__ma in__":
a=SomeClass()
a.SNC.SomeNeste dClassMethod()
I pretty much stole this idea from wxWindows.

Larry Bates
Syscon, Inc.
"Heiko Henkelmann" <he***@hhenkelm ann.de> wrote in message
news:c6******** **@online.de...
Please see the following example. Is there any other way to access
SomeAttribute from the nested class, without having to use the actual
name of the outer class?
class SomeClass:
SomeAttribute = 1

class SomeNestedClass :
SomeOtherAttrib ute = 2

def SomeNestedClass Method(cls):
print "%s%s" % (
SomeClass.SomeA ttribute
cls.SomeOtherAt tribute,
)
SomeNestedClass Method = classmethod(Som eNestedClassMet hod)

Thanx

Jul 18 '05 #2
Sorry for not mentioning this in the first place. I don't want to
instantiate the class. I much rather would to use it like:

SomeClass.SomeN estedClass.Some NestedClassMeth od()

Thanx
Larry Bates wrote:
I use something like:

class SomeClass:
SomeAttribute = 1

class SomeNestedClass :
SomeOtherAttrib ute = 2
def __init__(self, parent):
self.parent=par ent
return

def SomeNestedClass Method(self):
print "%s-%s" % (
self.parent.Som eAttribute,
self.SomeOtherA ttribute)

def __init__(self):
self.SNC=self.S omeNestedClass( self)
return

if __name__=="__ma in__":
a=SomeClass()
a.SNC.SomeNeste dClassMethod()
I pretty much stole this idea from wxWindows.

Larry Bates
Syscon, Inc.
Please see the following example. Is there any other way to access
SomeAttribu te from the nested class, without having to use the actual
name of the outer class?
class SomeClass:
SomeAttribute = 1

class SomeNestedClass :
SomeOtherAttrib ute = 2

def SomeNestedClass Method(cls):
print "%s%s" % (
SomeClass.SomeA ttribute
cls.SomeOtherAt tribute,
)
SomeNestedClass Method = classmethod(Som eNestedClassMet hod)

Thanx


Jul 18 '05 #3

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

Similar topics

0
1256
by: Michele Simionato | last post by:
Here is a hack to implement attribute access (say for a Web framework) the way I always wanted it to be. I say "hack" since it uses sys._getframe and it sets the locals of a class, which is not recommended. Still, it seems to work. I post the recipe here since since I would like to know if 1. it really works; 2. if yes, is there a chance that it will not work in future versions of Python; 3. will it work under Jython? Even if it turns...
9
1535
by: John Harrison | last post by:
Both gcc 3.3.1 and VC++ 7.1 compile the following code. struct Outer { struct Inner { int f() { return c; } }; private: static const int c;
5
2422
by: Fabio Rossi | last post by:
Hi! I'm learning C++ from the book "Thinking in C++". Now I'm reading about nested classes and access control. I have written this code #include <iostream> class Outer { private: int outer_data;
166
8579
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
4624
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other classes, as would be the case when making the method or field public? Thanks, Dennis
9
1853
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added to the order */ }
6
1664
by: MilanB | last post by:
Hello //----------------- I have following situation: public class Class1 { public struct Struct1 { public void Function1() {
2
2935
by: dobest03 | last post by:
Hi. Are there any way to access the integer member 'a' of outer structure from inner structure's member function func_inner()? See below the structure... Thanks. struct outer {
9
4672
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
0
7993
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
8270
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
6730
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3901
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2424
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
1
1511
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.