very strange problem in 2.4

John Zenger john_zenger at yahoo.com
Fri Apr 7 21:18:12 EDT 2006


Your list probably contains several references to the same object, 
instead of several different objects.  This happens often when you use a 
technique like:

list = [ object ] * 100

..because although this does make copies when "object" is an integer, it 
just makes references in other cases.

conor.robinson at gmail.com wrote:
> The Problem (very basic, but strange):
> 
> I have a list holding a population of objects, each object has 5 vars
> and appropriate funtions to get or modify the vars.  When objects in
> the list have identical vars (like all = 5 for var "a" and all = 10 for
> var "b" across all vars and objects) and i change
> 
> self.mylist[i].change_var_a(5)
> 
> to a new value, in this case var "a" in object i to 5, now all vars of
> type "a" in all objects in my list are changed to 5 instead of just var
> "a" in object mylist[i], which is my goal.
> 
> if i print self.mylist[i].return_var_a() right after I change var "a"
> in object i, I get the correct change, however when i print out the
> whole list, the last change to var "a" in the last object modified
> takes over for all objects in the list.
> 
> note: all the vars not being modified must be the same across all
> objects in the list, the var being modified need not be the same as the
> one before it in the list (but will be once just one of the identical
> object are changed).  The value changed in the last object var modified
> takes over for all object vars making them exactly identical.
> 
> ****If, for example, half the list has objects with random vars init.
> and the other half is identical, as above, and I perform the same
> operation, as above, to one of the identical var objects
> 
> self.mylist[i].change_var_a(5) (to an object that has identicals in the
> list)
> 
> all the identicals are changed in the same way as above, however the
> objects that have different var values are unchanged.
> 
> 
> 
> What is python doing? Am I missing something? Any ideas at all would be
> wonderful?
> 



More information about the Python-list mailing list