Inconsistent list/pointer problem

D

Doug Stell

I am having a problem with the corruption of a list. It occurs only
the first time that I call a function and never happens on subsequent
calls. Any suggestions would be appreciated.

I call the function, passing in a list as the input data. The function
must manipulate and operate on a copy of that list's data, without
altering the list in the calling routine.

def myFunc(listA):
listB = listA
work on & modify listB
return(listB)

The first time this function touches listB, listA is corrupted.
However, I get the right results from the function. On subsequent
calls to the function, listA is not corrupted further and the function
will always return the same wrong results, which would be correct
given the corruption of listA created in the first call.

I concluded that it appears that listB is still pointing at elements
of listA and I need to force Python to reassign those pointers to
point to copies of listA's elements.

I've tried copying listA to a tuple and then change the copy back to a
list. That sometimes works if the input data is a simple list. It does
not work if the input data is a list extracted from a list of lists.

listB = tuple(listA)
listB = list(listB)

I've tried building the copy of listA, element by element, but that
doesn't work.

listB = []
for x in listA:
listB.append(x)

I finally had to do some type changing during the element by element
copy and that does seem to work.

Thanks in advance for any suggestions. Doug
 
B

bearophileHUGS

Doug Stell:
The standard module copy has deepcopy, it's slow but it may be a
simple solution to your problem. A better solution is to look where
data is changed and fix that.

Bye,
bearophile
 
R

Richard Brodie

I call the function, passing in a list as the input data. The function
must manipulate and operate on a copy of that list's data, without
altering the list in the calling routine.

Then you will want to make a copy:
listB = copy.deepcopy( listA)
 
B

Bruno Desthuilliers

Doug Stell a écrit :
I am having a problem with the corruption of a list. It occurs only
the first time that I call a function and never happens on subsequent
calls. Any suggestions would be appreciated.

I call the function, passing in a list as the input data. The function
must manipulate and operate on a copy of that list's data, without
altering the list in the calling routine.

def myFunc(listA):
listB = listA
work on & modify listB
return(listB)

return is a statement, not a function. Please remove these useless (and
possibly harmful) parens.
The first time this function touches listB, listA is corrupted.

It's not. It's just that you did *not* copy listA - you just made listB
reference the same object.
I concluded that it appears that listB is still pointing at elements
of listA

It's even worse : both names listA and listB are pointing to the exact
same object.

listA = ['A', 'B', 'C']
listB = listA
assert listA is listB
and I need to force Python to reassign those pointers
s/pointers/references/

to
point to copies of listA's elements.

copy.deepcopy() is your friend then.
 
G

Gabriel Genellina

I am having a problem with the corruption of a list. It occurs only
the first time that I call a function and never happens on subsequent
calls. Any suggestions would be appreciated.

I call the function, passing in a list as the input data. The function
must manipulate and operate on a copy of that list's data, without
altering the list in the calling routine.

def myFunc(listA):
listB = listA
work on & modify listB
return(listB)

This article will help you understanding this behavior:
http://effbot.org/zone/python-objects.htm
 
E

Eduardo \EdCrypt\ O. Padoan

def myFunc(listA):
listB = listA
work on & modify listB
return(listB)

def my_func(listA):
listB = listA[:]
#work on & modify listB
return listB

Attribution makes the name t the left 'point' to the result of the
expression at the right.
In your myFunc the expersion at the right is the name listA, at it
willl eval to the list that this references. So you will end with to
references.
In my my_func, the expression at the right is a slicing of the list,
from begin to end (listA[:]). Slicing returns a new list, in this
example, with the same items of the original list. So you end with two
lists, as you wanted.


--
EduardoOPadoan (eopadoan->altavix::com)
Bookmarks: http://del.icio.us/edcrypt
Blog: http://edcrypt.blogspot.com
Jabber: edcrypt at jabber dot org
ICQ: 161480283
GTalk: eduardo dot padoan at gmail dot com
MSN: eopadoan at altavix dot com


--
EduardoOPadoan (eopadoan->altavix::com)
Bookmarks: http://del.icio.us/edcrypt
Blog: http://edcrypt.blogspot.com
Jabber: edcrypt at jabber dot org
ICQ: 161480283
GTalk: eduardo dot padoan at gmail dot com
MSN: eopadoan at altavix dot com
 
B

Bruno Desthuilliers

Eduardo "EdCrypt" O. Padoan a écrit :
def myFunc(listA):
listB = listA
work on & modify listB
return(listB)

def my_func(listA):
listB = listA[:]
#work on & modify listB
return listB


Won't do for the OP's needs - he wants to modify the objects contained
in listB without impacting the ones in listA (or at least that's what I
understand).
 
E

Eduardo \EdCrypt\ O. Padoan

Won't do for the OP's needs - he wants to modify the objects contained
in listB without impacting the ones in listA (or at least that's what I
understand).


Sorry. That is true - the items referenced on the [:] copy are the same as
in the original. Rereading what the OP msg, I think we agree.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top