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

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



Ian Mallett wrote:

If I have something like:
x = [1,2,3,4,5,6]
y = x
then changes to y change x.

That's a rather loose way of talking about it. What's
happening is that x and y refer to the same object,
so any changes made to that object will be seen through
both x and y.

The important things to understand are:

(1) Python variables always contain references to objects,
    not the objects themselves.

(2) The '=' operation is always reference assignment.
    It never copies any objects.

y = [x[0],x[1],x[2],x[3],x[4],x[5]]
...which doesn't change x when y is changed.

Also

(3) The [...] notation always constructs a new list
    object.

--
Greg