473,738 Members | 6,042 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 9710
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
1263
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
1538
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
2429
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
8642
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
4639
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
1861
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
1670
by: MilanB | last post by:
Hello //----------------- I have following situation: public class Class1 { public struct Struct1 { public void Function1() {
2
2938
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
4688
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
8968
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
8787
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
9334
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...
1
9259
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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...
1
6750
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
4569
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
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
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.