How to access outer class attribute from nested class

Discussion in 'Python' started by Heiko Henkelmann, Apr 27, 2004.

  1. 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
    Heiko Henkelmann, Apr 27, 2004
    #1
    1. Advertising

  2. Heiko Henkelmann

    Larry Bates Guest

    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
    Larry Bates, Apr 27, 2004
    #2
    1. Advertising

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

    >
    >
    >
    Heiko Henkelmann, Apr 27, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    15,297
    cbDevelopment
    Nov 13, 2005
  2. mrstephengross
    Replies:
    4
    Views:
    456
    Diez B. Roggisch
    Feb 27, 2008
  3. Richard Lee
    Replies:
    4
    Views:
    1,018
    James Kanze
    Mar 6, 2008
  4. DBak
    Replies:
    11
    Views:
    986
  5. Replies:
    2
    Views:
    223
Loading...

Share This Page