how do I pass values between classes?

Larry Bates larry.bates at websafe.com
Mon Nov 6 13:14:29 EST 2006


kath wrote:
> hi, Larry Bates .... thanks for the reply...
> 
>> You might consider doing it the same way wx passes things around.
>> When you instantiate the subclass pass the parent class' instance
>> as first argument to __init__ method.
> 
> Yes thats absolutely right..
> 
>> That way the subclass can
>> easily pass values back to the parent by using that pointer.
> 
> Could you please explain me this.. more clearly. I think it is much
> close to the solution.
> 
> 
> Thank you.
> regards, sudhir
> 
Just something like:

class foo:
    self.__init__(self, parent):
        self.parent=parent

class bar:
    self.__init__(self, parent):
        self.parent=parent
        self.foo=foo(self)


class myclass:
    self.__init__(self, parent):
        self.parent=parent
        self.bar=bar(self)

Now in foo I can reference things from myclass easily
self.parent.parent.  Or setattr(self.parent.parent, value).

Hope this helps.

-Larry



More information about the Python-list mailing list