Is this a refrence issue?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Dec 28 18:46:23 EST 2005


On Wed, 28 Dec 2005 14:40:45 -0800, Carl J. Van Arsdall 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 *is not a copy at all*.

The *names* "tmp" and "myList" both are bound to the *same* object. They
are two names for the same object.

tmp = myList[:] is a shallow copy of myList.

tmp = copy.deepcopy(myList) makes a copy of myList, *and* copies of
everything inside myList.



-- 
Steven.




More information about the Python-list mailing list