Is this a refrence issue?

Jean-Paul Calderone exarkun at divmod.com
Wed Dec 28 17:48:53 EST 2005


On Wed, 28 Dec 2005 14:40:45 -0800, "Carl J. Van Arsdall" <cvanarsdall at mvista.com> wrote:
>KraftDiner wrote:
>> I understand that everything in python is a refrence....
>>
>> I have a small problem..
>>
>> I have a list and want to make a copy of it and add an element to the
>> end of the new list,
>> but keep the original intact....
>>
>> so:
>> tmp = myList
>>
>
>tmp = myList is a shallow copy
>

"tmp = myList" isn't a copy at all.  A shallow copy is like this:

  tmp = myList[:]

or like this:

  import copy
  tmp = copy.copy(myList)

This is as opposed to a deep copy, which is like this:

  import copy
  tmp = copy.deepcopy(myList)

What "tmp = myList" does is to create a new *reference* to the 
very same list object.

Jean-Paul



More information about the Python-list mailing list