[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: is != == (Re: [pygame] Sticky Variables: Terrain Loading)



On Wed, Mar 12, 2008 at 01:06:07PM -0700, Ian Mallett wrote:
> Hmm.
> This might be associated with another problem I've been having.
> NOT really tried--not near a python interpreter...
> If I have something like:
> x = [1,2,3,4,5,6]
> y = x
> then changes to y change x.  So I've had to:
> y = [x[0],x[1],x[2],x[3],x[4],x[5]]
> ...which doesn't change x when y is changed.


There is an easier way to do this.

  from copy import deepcopy
  x = [1,2,3,4,5,6]
  y = deepcopy(x)

Of course, in this example, the deep copy is pointless and wasteful no 
matter how you implement it, since integer constants are cached 
internally (as you know already from reading this thread)

But as for the subject of copies vs references, be absolutely certain 
that a copy is really what you want. Many newcomers to python don't 
understand how rarely a copy is really necessary. Personally, almost 
every time I have ever used the copy module, I have later removed it 
after understanding my own code better and realizing it was unneeded.

---
James Paige