Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can a method in one class change an object in another class?

Reply
Thread Tools

Can a method in one class change an object in another class?

 
 
Stewart Midwinter
Guest
Posts: n/a
 
      03-06-2005
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.

---
#objtest.py

class first:
def __init__(self):
a = 'a'
self.a = a
print self.a

def update(self):
print 'initially, a is', self.a
self.a = second(self.a)
print 'afterwards, a is', self.a

class second:
def __init__(self, a):
pass

def __call__(self, a):
a = 'aa'
return a

if __name__ == '__main__':
app = first()
app.update()

thanks,
--
Stewart Midwinter
http://www.velocityreviews.com/forums/(E-Mail Removed)
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Harlin Seritt
Guest
Posts: n/a
 
      03-06-2005
Here's what I came up with:

#objtest.py


class first:
def __init__(self):
a = 'a'
self.a = a
print self.a


def update(self):
print 'initially, a is', self.a
self.a = second(self.a)
print 'afterwards, a is', self.a.call(self.a)


class second:
def __init__(self, a):
pass


def call(self, a):
a = 'aa'
return a


if __name__ == '__main__':
app = first()
app.update()

Not sure if this is what you are wanting though.

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-06-2005
> 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?



Sure it can. But your code shows that you suffer from a fundamental
misunderstanding on how variables and values work in python. Don't be to
worried about that, it happens to quite a few people.

A variable in python is just a name refering to an object. So this

>>> a = 'a'
>>> b = a
>>> print a, b

a a


will make a and b both refer to the string 'a'. Now assigning a different
value to b will not affect the binding of a:

>>> b = 10
>>> print a, b

a 10

So that is the reason you obeserve the behaviour you've seen. Now the
question is how to accomplish your desired goal? The answer is simple:
don't rebind a value to a name - alter the value! In python, there is a
distinction between mutable objects and immutable ones. Strings and numbers
are of the latter kind, lists and dicts and objects of the former. So if we
changed our example slighly, things start working as you'd expect:

>>> a = ['a']
>>> b = a
>>> print a,b

['a'] ['a']
>>> b[0] = 10
>>> print a, b

[10] [10]

So if you pass the same _mutable_ object to two objects, and one of them
changes it, the other one will see the changes:

class A:
def __init__(self):
self.a_list = [0]

class B:
def __init__(self, l):
self.l = l

def foo(self):
self.l.append(100)

a = A()
b = B(a.a_list)
b.foo()
print a.a_list

-> [0, 100]


There are some resources on the web that explain this in more thourough
detail - but curretntly I have trouble finding them. Search this newsgroup.
--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
Lee Harr
Guest
Posts: n/a
 
      03-06-2005
On 2005-03-06, Stewart Midwinter <(E-Mail Removed)> 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')

 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      03-06-2005
Stewart Midwinter 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?


Diez and Lee have shown you two ways to do this.

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


This is almost what your example does, but you have made a small error. See below.

> 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?


Yes, it is 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.
>
> ---
> #objtest.py
>
> class first:
> def __init__(self):
> a = 'a'
> self.a = a
> print self.a
>
> def update(self):
> print 'initially, a is', self.a
> self.a = second(self.a)


The line above is creating an instance of second and assigning it to self.a. What you want to do is
create an instance of second, *call* it, and assign the result to self.a. So you should have
self.a = second(self.a)(self.a)

The self.a parameter passed to second is never used. If you change second.__init__ to
def __init__(self):
pass

then the call in update() will be
self.a = second()(self.a)

Kent
> print 'afterwards, a is', self.a
>
> class second:
> def __init__(self, a):
> pass
>
> def __call__(self, a):
> a = 'aa'
> return a
>
> if __name__ == '__main__':
> app = first()
> app.update()
>
> thanks,
> --
> Stewart Midwinter
> (E-Mail Removed)
> (E-Mail Removed)

 
Reply With Quote
 
stewart.midwinter@gmail.com
Guest
Posts: n/a
 
      03-07-2005
thanks guys. Three good answers, each slightly different, but giving me
good food for thought.

Obviously my example was a trivial one, but I wanted to isolate the
behaviour I'm seeing in my real app. I now have some good ideas for
moving forward!

cheers
S

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to access object of one class in another class xodepp shrestha Java 3 02-21-2013 05:45 PM
injecting an instance method into another class' class method Chuck Remes Ruby 3 08-12-2010 07:15 PM
Adding method from one class to another class or to instance ofanother class marekw2143 Python 3 07-25-2009 08:33 PM
While executing the class definition which object is referenced bythe first argument of the class method, Y r Object attributes not allowed asdefault arguments Krishna Python 4 03-07-2008 09:44 PM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM



Advertisments