473,398 Members | 2,525 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,398 software developers and data experts.

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:
SomeOtherAttribute = 2

def SomeNestedClassMethod(cls):
print "%s%s" % (
SomeClass.SomeAttribute
cls.SomeOtherAttribute,
)
SomeNestedClassMethod = classmethod(SomeNestedClassMethod)

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

class SomeClass:
SomeAttribute = 1

class SomeNestedClass:
SomeOtherAttribute = 2
def __init__(self, parent):
self.parent=parent
return

def SomeNestedClassMethod(self):
print "%s-%s" % (
self.parent.SomeAttribute,
self.SomeOtherAttribute)

def __init__(self):
self.SNC=self.SomeNestedClass(self)
return

if __name__=="__main__":
a=SomeClass()
a.SNC.SomeNestedClassMethod()
I pretty much stole this idea from wxWindows.

Larry Bates
Syscon, Inc.
"Heiko Henkelmann" <he***@hhenkelmann.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:
SomeOtherAttribute = 2

def SomeNestedClassMethod(cls):
print "%s%s" % (
SomeClass.SomeAttribute
cls.SomeOtherAttribute,
)
SomeNestedClassMethod = classmethod(SomeNestedClassMethod)

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.SomeNestedClass.SomeNestedClassMethod()

Thanx
Larry Bates wrote:
I use something like:

class SomeClass:
SomeAttribute = 1

class SomeNestedClass:
SomeOtherAttribute = 2
def __init__(self, parent):
self.parent=parent
return

def SomeNestedClassMethod(self):
print "%s-%s" % (
self.parent.SomeAttribute,
self.SomeOtherAttribute)

def __init__(self):
self.SNC=self.SomeNestedClass(self)
return

if __name__=="__main__":
a=SomeClass()
a.SNC.SomeNestedClassMethod()
I pretty much stole this idea from wxWindows.

Larry Bates
Syscon, Inc.
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:
SomeOtherAttribute = 2

def SomeNestedClassMethod(cls):
print "%s%s" % (
SomeClass.SomeAttribute
cls.SomeOtherAttribute,
)
SomeNestedClassMethod = classmethod(SomeNestedClassMethod)

Thanx


Jul 18 '05 #3

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

Similar topics

0
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...
9
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
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...
166
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
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...
9
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...
6
by: MilanB | last post by:
Hello //----------------- I have following situation: public class Class1 { public struct Struct1 { public void Function1() {
2
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
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...

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.