numpy: pass by reference does not work on itself

Viewed 118

I tried the following:

p=np.array([[1,1,1],[2,2,2],[3,3,3]])
p[0,:] = p[1,:]
y = p[1,:]
print(p)
p[1,1] = 4
print(p)
print(y)

As you can see the output is:

[[2 2 2]
 [2 2 2]
 [3 3 3]]
[[2 2 2]
 [2 4 2]
 [3 3 3]]
[2 4 2]

So when I assigned the second row of p to y it was passed by reference. When I assigned the second row of p to the first row of p it was passed by copy. Why is that the case?

The output I expected is:

[[2 2 2]
 [2 2 2]
 [3 3 3]]
[[2 4 2]
 [2 4 2]
 [3 3 3]]
[2 4 2]
1 Answers
Related