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

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



I remember having issues at one point with references as I was confused at why changes to one array changed the other. My work-around has simply been:
x = [1, 2, 3]
y = x[:]

Nice to see there are other methods though.

On Wed, Mar 12, 2008 at 9:04 PM, Greg Ewing <greg.ewing@xxxxxxxxxxxxxxxx> wrote:
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




--
This, from Jach.

How many programmers does it take to change a light bulb?
None. It's a hardware problem.

How many Microsoft programmers does it take to change a light bulb?
None. Microsoft just declared darkness as the newest innovation in cutting-edge technology.