473,473 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Syntax: pointers versus value

Simple question but I can't find the answer. Using an example:

listA = [1 ,2]
listB = listA
listB.append(3)
#listA now is [1,2,3]

I want to avoid listA's value to change. Clearly, "listB = listA"
creates the problem. listB and listA become two pointers to the same
object. How then, do I duplicate the two objects; ie make a copy of the
object that listA is pointing to and have listB point to that object.

I'm new to python and I love it.

Thank you,

Jul 18 '05 #1
5 1437
On Wed, 30 Jul 2003 12:46:43 -0400, Danny Castonguay
<ca*****@mathstat.concordia.ca> wrote:

Hi!
Clearly, "listB = listA"
creates the problem. listB and listA become two pointers to the same
object.
Exactly.
How then, do I duplicate the two objects; ie make a copy of the
object that listA is pointing to and have listB point to that object.


Use:
listB = listA[:]

Cheers,

Tino

Jul 18 '05 #2
Tino Lange wrote:
Use:
listB = listA[:]


--------------------------------------------------------------------
In the example I have given, it does solve the problem. However, for
some reason I don't understand, it doesn't work in the next example:

def ps_of_missing_edges(initial_graph, missing_edges):
num_m_e = len(missing_edges) #number of missing_edges
num_e_pset = 2**num_m_e
graphs = []
for i in range(0,num_e_pset): #iteration will stop at 2^num-1
temp_i = i #use temp_i to find i's bit values
new_graph = initial_graph[:]
print 'initial_graph is ' + str(initial_graph)
for j in range (0,num_m_e):
if temp_i%2 == 1:
new_graph[missing_edges[j][0]-1].append(missing_edges[j][1])
temp_i = temp_i >> 1
graphs.append(new_graph)
return graphs

--------------------------------------------------------------

the output when I call:
ps_of_missing_edges([[2,3],[1],[1]], [[2,3],[3, 2]])
is:
initial_graph is [[2, 3], [1], [1]]
initial_graph is [[2, 3], [1], [1]]
initial_graph is [[2, 3], [1, 3], [1]]
initial_graph is [[2, 3], [1, 3], [1, 2]]

--------------------------------------------------------------
Therefore, somehow initial_graph's value changes even though the
assignment is new_graph = initial_graph[:]

Assuming that the assignment does it's job, then I don't see how
initial_graph's value changes.
Thank you,

Jul 18 '05 #3
On Wed, 30 Jul 2003 13:46:29 -0400, Danny Castonguay
<ca*****@mathstat.concordia.ca> wrote:
new_graph = initial_graph[:]
initial_graph is [[2, 3], [1], [1]]
initial_graph is [[2, 3], [1], [1]]
initial_graph is [[2, 3], [1, 3], [1]]
initial_graph is [[2, 3], [1, 3], [1, 2]]


Hi!

Now you have lists in lists. This is another situation.

new_graph = initial_graph[:] will really make a copy of the outer list
- but this outer list contains no values but once again pointers to
other objects.

Use the copy-module for nested copy things like that:
http://www.python.org/doc/current/lib/module-copy.html

Cheers,

Tino

Jul 18 '05 #4
Danny Castonguay <ca*****@mathstat.concordia.ca> writes:
Simple question but I can't find the answer. Using an example: [...] object. How then, do I duplicate the two objects; ie make a copy of
the object that listA is pointing to and have listB point to that
object.

[...]

http://www.python.org/doc/FAQ.html#4.38
John
Jul 18 '05 #5
>>>>> "Danny" == Danny Castonguay <ca*****@mathstat.concordia.ca> writes:

Danny> I want to avoid listA's value to change.

There are several ways

listB = listA[:]

listB = [x for x in listA]

import copy
listB = copy.copy(listA)
listB = copy.deepcopy(listA)

These don't all do the same thing, but all satisfy the example you
posted, eg
listA = [1 ,2]
listB = listA[:]
listB.append(3)
listA [1, 2]

So far so good, but consider this case
listA = [[],1]
listB = listA[:]
listB[0].append(12) # changing the list at listB[0]
listB [[12], 1] listA [[12], 1]

Sneaky, eh? So the 'listB = listA[:]' made a copy of the list, but
not of the elements of the list. This is a shallow copy, and you
should get the same behavior from copy.copy.

Note that this is related to mutable and immutable objects in python.
In the example above, I didn't replace the first item of the list, I
changed it. Since both listA and listB contained the same object as
their first element, changing one changes the other. If I had
replaced that object in listB, nothing would have happened to listA
listA = [[],1]
listB = listA[:]
listB[0] = 12 # replacing the object, not changing it
listB [12, 1] listA [[], 1]
If you want to avoid this, use deepcopy.
import copy
listA = [[],1]
listB = copy.deepcopy(listA)
listB[0].append(12)
listB [[12], 1] listA

[[], 1]

The behavior of mutable objects bites everyone a few times when they
begin learning python, so welcome to the club!

John Hunter

Jul 18 '05 #6

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

Similar topics

8
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
8
by: tom | last post by:
I am new to SQL administration. >From a list of IDs that are the primary key in one table (i.e. Customer Table), I want to make changes in tables that use those IDs as a foreign key. ...
4
by: Aaron Walker | last post by:
Greetings, I'm attempting to write my first *real* template function that also deals with a map of strings to member function pointers that is making the syntax a little tricky to get right. ...
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
46
by: TTroy | last post by:
Hi, I'm just wondering why people/books/experts say "the function returns a pointer to.." or "we have to send scanf a pointer to.." instead of "the function returns the address of.." or "we have...
2
by: bor_kev | last post by:
Hi, First of all, i want to use the new managed class syntax and STL.NET under Microsoft Visual (C++) Studio 2005 Beta. I read in a Microsoft...
6
by: stork | last post by:
C++ is always evolving. Within C++, there tend to be two communities. There is the community pushing for performance features, getting ever closer to the metal, and then, there is the...
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
9
by: parag_paul | last post by:
Hi all I am seeing the following code in one place standalone line in some function , { (void*) new (h) Class_Name(xip, virobj, type, true); } Does it make h an object of the Class_Name...
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
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
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,...
0
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...
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.