very strange problem in 2.4

C

conor.robinson

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.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, which is my goal.

if i print self.mylist.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.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?
 
J

John Zenger

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

Steven D'Aprano

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
 
B

Ben Cartwright

John said:
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
 
F

Fredrik Lundh

John said:
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>
 
B

Bruno Desthuilliers

(e-mail address removed) 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.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, 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
 
C

conor.robinson

Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist) != 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
 
C

conor.robinson

Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist) != 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
 
C

conor.robinson

Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist) != 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
 
C

conor.robinson

Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist) != 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
 
C

conor.robinson

Ok, so I found out that even though mylist[] and all objects in it were
fine ie id(mylist) != 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top