By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

How to access outer class attribute from nested class

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.