Is this a refrence issue?

James Tanis jtanis at pycoder.org
Wed Dec 28 17:55:42 EST 2005


On 12/28/05, 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.append(something)
> > print tmp, myList
> >
> > should be different...
> >
> Therefore it shouldn't be different.
>
> What you could do is make a second list
> tmp = []
>
> And then use the extend method:
>
> tmp.extend(myList)
> tmp.append(someEntry)
>
> This would give you what you want, and there's more than one way to do this.
>

Such as from the python docs..

import copy

x = copy.copy(y)        # make a shallow copy of y
x = copy.deepcopy(y)    # make a deep copy of y

> -carl
>
> --
>
> Carl J. Van Arsdall
> cvanarsdall at mvista.com
> Build and Release
> MontaVista Software
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


--
James Tanis
jtanis at pycoder.org
http://pycoder.org



More information about the Python-list mailing list