Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to access outer class attribute from nested class

Reply
Thread Tools

How to access outer class attribute from nested class

 
 
Heiko Henkelmann
Guest
Posts: n/a
 
      04-27-2004
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
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      04-27-2004
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" <> wrote in message
news:c6m9dg$die$...
> 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



 
Reply With Quote
 
 
 
 
Heiko Henkelmann
Guest
Posts: n/a
 
      04-27-2004
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

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Access to private data type of an outer class from inner class agentprog@gmail.com C++ 2 04-02-2013 04:48 PM
Want - but cannot get - a nested class to inherit from outer class DBak Python 11 03-08-2008 01:38 PM
nested class inheriting from outer class Richard Lee C++ 4 03-06-2008 09:54 AM
Instance of inherited nested class in outer class not allowed? mrstephengross Python 4 02-27-2008 10:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57