Is this a refrence issue?

Mike Meyer mwm at mired.org
Wed Dec 28 17:45:22 EST 2005


"KraftDiner" <bobrien18 at yahoo.com> writes:
> I understand that everything in python is a refrence....

Correct.

> I have a small problem..

Maybenot so small.

> 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.append(something)
> print tmp, myList
>  
> should be different...

They won't be, because the assignment statement just binds the name on
the left to the value on the right, meaning they'll both point to the
same object. To get a copy, you have to make a copy. The easy way is:

tmp = list(myList)

However, this won't coppy the things *in* myList. If you want that you
want the copy module's deepcopy function.

     <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list