To supplement John's resource (which explains that the behavior you 
indicate is indeed a property of the language):

Currently (and in the version you're using), range indexing such as `x[:]` 
returns a copy of `x`. So does applying an operation like `y * 4`. That's 
why writing `y = x[:]` or `y = y * 4` doesn't reassign `y` in such a way 
that subsequent modifications to `y` impact `x`.

On Saturday, July 25, 2015 at 11:13:33 AM UTC-4, Christopher Fisher wrote:
>
> I am writing a program (in .3.1) that takes a one dimensional array of 
> indices (x) and creates a new one dimensional array of indices  (x = y) and 
> switches a given pair of elements in the new array. For some reason, the 
> original array (x) changes when I change the new array (see below). 
>
> In [16]:
>
> x = [1:5]
>
> y = x
>
> y[[3;4]] = y[[4;3]]
>
> [x y]
>
> Out[16]:
>
> 5x2 Array{Int64,2}:
>  1  1
>  2  2
>  4  4
>  3  3
>
>
>
> However, if I change y = x to y = x[:], the problem does not occur, even 
> though x and x[:] appear to be the same type.
>
>
> In [15]:
>
> x = [1:5]
>
> y = x[:]
>
> y[[3;4]] = y[[4;3]]
>
> [x y]
>
> Out[15]:
>
> 5x2 Array{Int64,2}:
>  1  1
>  2  2
>  3  4
>  4  3
>  5  5
>
>
> The code below shows that this does not seem to be a general property of the 
> language. As in the previous cases, I define y as x and change y. 
>
>
> In [13]:
>
> x = [1:5]
>
> y = x
>
> y = y*4
>
> [x y]
>
> Out[13]:
>
> 5x2 Array{Int64,2}:
>  1   4
>  2   8
>  3  12
>  4  16
>  5  20
>
>
>
> Certainly, I could use y = x[:] or simply change the original array. 
> Nonetheless, the inconsistency seems undesirable from my point of view. Is 
> this a bug? Any help would be appreciated. 
>
>
>

Reply via email to