473,748 Members | 7,377 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 1258
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_a ttrib = 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2839
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 mgmt system. I have 3 tables for the orders: TicketsOpen, TicketsVoided, and TicketsResolved. When one wants to void a ticket, they click it, choose Void, and it is copied to the TicketsVoided table, then removed from the TicketsOpen. And I can...
6
8533
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 of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
2
1784
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 to perform some calculations. After some number of iterations, randomly, I'll get an uncaught NullValueException error on one of these calls, as if the class name is being treated as an object reference and is null. Here is some psuedo-code to...
5
1692
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 have no clue why. But if I store the MethodInfo in a local variable I works as expected. I do not understand why this is so, shouldn't both ways be semantically equal?
2
1500
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 = {{"E", "en-GB"}, {"P", "pt-PT"}} Response.Write("1")
12
20699
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
4112
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 String, Optional ByVal ConnectionParamList As String = Nothing) As String
11
1849
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 and k but if I debug and watch the change of the variable after each command. When the sencond loop happends for k, the values of vs change and are set to be equal some values of k. Specifically, v is
4
2100
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 this way like: public class UiMsg { public enum MsgType { StatusOk }; public MsgType Type;
112
4742
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 work. To rule out all possible factors, I made up a dummy page for an index.html to upload, along the lines of <html><head><title></title></ head><body></body></html>.; the image-swap itself is your basic <img src="blah.png"...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6795
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.