How to access outer class attribute from nested class

H

Heiko Henkelmann

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
 
L

Larry Bates

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.
 
H

Heiko Henkelmann

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top