473,473 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing by reference? An copied array question...

3 New Member
Hi guys. I'm brand new to this forum, so if I'm messing up by starting my own thread, I'm sorry. But this array problem I'm running into has got me hitting a brick wall.

I'm in Python, and I've imported Numeric. And I'm on Fedora.

I'm trying to make a copy of a 3x3 array, modify the copy, then subtract the original array from the copy to see what's left. But every time I change the copy, the original changes too, so the final difference between the two 3x3 arrays is 0 in every element.

I looked around online and it looks like Python passes by reference, right? So whatever I do to the "copied" array is passed back to the original. I saw somewhere that

Expand|Select|Wrap|Line Numbers
  1. ..newarray = oldarray[:]..
was supposed to make a physical copy and avoid the problem, but it didn't work for me.

Here's what I'm running:

Expand|Select|Wrap|Line Numbers
  1. ..sitewater=masterwater[:]
  2. print masterwater
  3. print sitewater
  4. sitewater[2,0]=3
  5. print sitewater
  6. print masterwater..
and the output is:

Expand|Select|Wrap|Line Numbers
  1. ..[[  2.157016   0.931734  18.799198]
  2.  [  1.484143   2.309928  18.915375]
  3.  [  1.283086   1.359119  18.840746]]
  4. [[  2.157016   0.931734  18.799198]
  5.  [  1.484143   2.309928  18.915375]
  6.  [  1.283086   1.359119  18.840746]]
  7. [[  2.157016   0.931734  18.799198]
  8.  [  1.484143   2.309928  18.915375]
  9.  [  3.         1.359119  18.840746]]
  10. [[  2.157016   0.931734  18.799198]
  11.  [  1.484143   2.309928  18.915375]
  12.  [  3.         1.359119  18.840746]]..
Any ideas? Any advice on how to better ask questions would be appreciated too!

Thanks,
pholvey
Jun 17 '08 #1
2 1262
jlm699
314 Contributor
Expand|Select|Wrap|Line Numbers
  1. import copy
  2. newObj = copy.deepcopy(oldObj)
  3.  
this will prevent the "shallow copy" problem of which you speak.
Jun 17 '08 #2
pholvey
3 New Member
Looks like it worked just fine. Thanks guys!

pholvey

Expand|Select|Wrap|Line Numbers
  1. import copy
  2. newObj = copy.deepcopy(oldObj)
  3.  
this will prevent the "shallow copy" problem of which you speak.
Jun 17 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: lawrence | last post by:
Those of you with backgrounds with the C language will laugh at my mistake, but those of you, like myself, who deal mostly with PHP should be warned about passing variables as references -...
15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
5
by: blue | last post by:
We often get connection pooling errors saying that there are no available connections in the pool. I think the problem is that we are passing around open readers all over the place. I am...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
11
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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
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
muto222
php
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.