467,146 Members | 1,060 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

very strange problem in 2.4

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?

Apr 8 '06 #1
  • viewed: 1029
Share:
10 Replies
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.

co************@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?

Apr 8 '06 #2
On Fri, 07 Apr 2006 21:18:12 -0400, John Zenger wrote:
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.


Wrong. It always makes references.
L = [1]*3
id(L[0]), id(L[1]), id(L[2]) (155972920, 155972920, 155972920)

This isn't a caching issue either, it also happens for objects which
aren't cached:
x = 4591034.56472
y = 4591034.56472
x == y True x is y False L = [x]*3
x is L[0] is L[1] is L[2] True y is L[0]

False

--
Steven.

Apr 8 '06 #3
John Zenger wrote:
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


This is most likely what's going on. To the OP: please post the
relevant code, including how you create mylist and the definitions of
change_var_a and return_var_a. I suspect you're doing something like
this:
\ class C(object):
def __init__(self, x):
self.x = x
def __repr__(self):
return 'C(%r)' % self.x
mylist = [C(0)]*3 + [C(1)]*3
mylist [C(0), C(0), C(0), C(1), C(1), C(1)] mylist[0].x = 2 [C(2), C(2), C(2), C(1), C(1), C(1)]

When you should do something like:
mylist = [C(0) for i in range(3)] + [C(1) for i in range(3)] [C(0), C(0), C(0), C(1), C(1), C(1)] mylist[0].x = 2

[C(2), C(0), C(0), C(1), C(1), C(1)]

--Ben

Apr 8 '06 #4
John Zenger wrote:
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.


it always creates new references.

the only thing that distinguishes immutable objects (like integers) from
mutable objects (like lists) is that integers don't have any methods that
let you modify their contents.

there's no "this object is mutable" flag inside the object, and there's no
code in the list multiply operation, or anywhere else, that looks for such
a flag.

</F>

Apr 8 '06 #5
co************@gmail.com a écrit :
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.
Which are probably not necessary:
http://dirtsimple.org/2004/12/python-is-not-java.html

(in short: Python as a mechanism named "properties" that allow you to
"gateway" attribute access thru hiddens getter and setter).
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.
(snip)
What is python doing? Am I missing something? Any ideas at all would be
wonderful?


It would have helped if you had posted your code. Anyway, at least two
things could lead to the behaviour you describe:

1/ you have class attributes instead of instance attributes, ie:

class MyClass(object):
# this attribute belongs to the class
class_attrib = 42

def __init__(self, instance_attrib):
# this attribute belongs to the instance
self.instance_attrib = instance_attrib

2/ you in fact have in your list multiple references to the same instance.

My 2 cents
Apr 8 '06 #6
Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist[i]) != id(mylist[all others]) what was happening is
that during a reproduction function a shallow copies were being made
making all offspring (genetic algorithm) have different
id(mylist[0..n]), however the actual attributes were referenced for
offspring on the same parent(s). Fixed it with a deepcopy and used
properties insead of java like get sets.

thanks for all the help,
Conor

Apr 10 '06 #7
Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist[i]) != id(mylist[all others]) what was happening is
that during a reproduction function a shallow copies were being made
making all offspring (genetic algorithm) have different
id(mylist[0..n]), however the actual attributes were referenced for
offspring on the same parent(s). Fixed it with a deepcopy and used
properties insead of java like get sets.

thanks for all the help,
Conor

Apr 10 '06 #8
Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist[i]) != id(mylist[all others]) what was happening is
that during a reproduction function a shallow copies were being made
making all offspring (genetic algorithm) have different
id(mylist[0..n]), however the actual attributes were referenced for
offspring on the same parent(s). Fixed it with a deepcopy and used
properties insead of java like get sets.

thanks for all the help,
Conor

Apr 10 '06 #9
Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist[i]) != id(mylist[all others]) what was happening is
that during a reproduction function a shallow copies were being made
making all offspring (genetic algorithm) have different
id(mylist[0..n]), however the actual attributes were referenced for
offspring on the same parent(s). Fixed it with a deepcopy and used
properties insead of java like get sets.

thanks for all the help,
Conor

Apr 10 '06 #10
Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist[i]) != id(mylist[all others]) what was happening is
that during a reproduction function a shallow copies were being made
making all offspring (genetic algorithm) have different
id(mylist[0..n]), however the actual attributes were referenced for
offspring on the same parent(s). Fixed it with a deepcopy and used
properties insead of java like get sets.

thanks for all the help,
Conor

Apr 10 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Google Mike | last post: by
2 posts views Thread by Shapper | last post: by
11 posts views Thread by VijaKhara@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.