get attribute from a parent class

Larry Bates lbates at swamisoft.com
Mon Aug 2 10:40:32 EDT 2004


I normally do something like this:

class A:
    def __init__(self, parent):
    self.parent=parent
    #
    # Access parent's variables by using
    # self.parent.<attribute>.
    #
    self.var = parent.var2
    t1=self.parent.var1
    t2=self.parent.var2
    return

class B:
    def __init__(self):
    self.var1=0
    self.var2=1
    self.classA=A(self)
    #
    # Access classA's attributes by using
    # self.classA.<attribute>
    #
    t1=A.var
    return

HTH,
Larry Bates
Syscon, Inc.

"Steve" <steve at hotmail.com> wrote in message
news:410d9a13$1 at clarion.carno.net.au...
> Hi,
>
> I have a nested class declaration such as:
>
> class A:
> def __init__(self):
> self.var = "A's variable"
> # end def
>
> class B:
> def __init__(self):
> self.var2 = "B's variable"
> # end def
> # end class B
> # end class A
>
>
> What I want to do is to be able to get class A's 'var' variable from the
> nested class B. For example, I want to be able to do something like:
> print "I can see you %s" % a.var
>
>
> but... I don't want to make 'var' a class variable. I want it to be an
> instance variable but still be viewable by the inner class B. Is this
> possible? Any suggestions? Thanks
>
> Steve
>





More information about the Python-list mailing list