How to access outer class attribute from nested class

Larry Bates lbates at swamisoft.com
Tue Apr 27 15:03:14 EDT 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" <heiko at hhenkelmann.de> wrote in message
news:c6m9dg$die$1 at 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





More information about the Python-list mailing list