473,513 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
10 1225
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2819
by: Google Mike | last post by:
I have RH9 and am using the PHP and MySQL that came with it. I was doing fine with all manner of my web pages for this app until I started having this very strange problem. It's a work order...
6
8503
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
2
1766
by: TB | last post by:
I am seeing a very strange problem as follows... I have a loop where a fair amount of processing is going on and near the top of the loop I access a class that has only static helper functions...
5
1669
by: cody | last post by:
I have a very funny/strange effect here. if I let the delegate do "return prop.GetGetMethod().Invoke(info.AudioHeader, null);" then I get wrong results, that is, a wrong method is called and I...
2
1491
by: Shapper | last post by:
Hello, I have this code: Dim cultureList(,) As String = {{"E", "en-GB"}, {"P", "pt-PT"}} Select Case Session("culture") Case "pt-PT" ... Dim cultureList(,) As String =...
12
20612
by: John | last post by:
I can't get my head around this! I have the following code: <% .... Code for connection to the database ... .... Code for retrieving recordset ... If Not rs.EOF Then ... Do something...
2
4095
by: peter | last post by:
Hi, I have very strange situation but first description ;) I have: 1) project in VB.NET, in this f.e. 1 function: Public Function Login(ByVal UserName As String, ByVal UserPassword As...
11
1827
by: VijaKhara | last post by:
Hi all, I just write a very simple codes in C and vthere is a very strange bug which I cannot figure out why. The first loop is for v, and the second for k. There is no relationship between v...
4
2079
by: Gotch | last post by:
Hi, I'm getting a very strange behaviour while running a project I've done.... Let's expose it: I've two projects. Both of them use a Form to do some Gui stuff. Other threads pack up messages...
112
4585
by: Prisoner at War | last post by:
Friends, your opinions and advice, please: I have a very simple JavaScript image-swap which works on my end but when uploaded to my host at http://buildit.sitesell.com/sunnyside.html does not...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.