On 2005-03-06, Stewart Midwinter <[EMAIL PROTECTED]> wrote: > I've got an app that creates an object in its main class (it also > creates a GUI). My problem is that I need to pass this object, a > list, to a dialog that is implemented as a second class. I want to > edit the contents of that list and then pass back the results to the > first class. So my question is, can a method in one class change an > object in another class? > > If the answer is no, I suppose I could pass in the list as an argument > when I create the second class, then return the contents of the list > when I end the methods in that second class. > > alternatively, I could make the list a global variable, then it would > be available to all classes. I have a nagging feeling though that > global variables are to be avoided on general principle. Is this > correct? > > Here's a simple example app that tries to have one class change the > object in another class. It doesn't give the behaviour I want, > though. >
Depends a bit on who is updating who and which is created first and which needs references to which. Maybe like this... > --- > #objtest.py > class first: def __init__(self, a): self.a = a print 'a initialized to', self.a self.updater = second(self) def update(self, a='aa'): print 'initially, a is', self.a self.updater.do_update(a) print 'afterwards, a is', self.a class second: def __init__(self, lst): self.lst = lst def do_update(self, a): self.lst.a = a if __name__ == '__main__': lst = first('a') lst.update() # or ... dlg = second(lst) lst.update('aaa') -- http://mail.python.org/mailman/listinfo/python-list