I have written the following code with python lists
# python lists
vc = [1,2,3,4]
print('original array')
print(hex(id(vc)))
print([hex(id(vc[i])) for i in range(len(vc))])
print(vc)
# --
g = vc[1:3]
print('array slice')
print(hex(id(g)))
print([hex(id(g[i])) for i in range(len(g))])
print(g)
# --
g[:] = [-1,-2]
print('original array')
print(hex(id(vc)))
print([hex(id(vc[i])) for i in range(len(vc))])
print(vc)
# --
print('array slice')
print(hex(id(g)))
print([hex(id(g[i])) for i in range(len(g))])
print(g)
that produces the expected output
original array
0x211acca9d48
['0x7ffc4ffbb350', '0x7ffc4ffbb370', '0x7ffc4ffbb390', '0x7ffc4ffbb3b0']
[1, 2, 3, 4]
array slice
0x211acc69e88
['0x7ffc4ffbb370', '0x7ffc4ffbb390']
[2, 3]
original array
0x211acca9d48
['0x7ffc4ffbb350', '0x7ffc4ffbb370', '0x7ffc4ffbb390', '0x7ffc4ffbb3b0']
[1, 2, 3, 4]
array slice
0x211acc69e88
['0x7ffc4ffbb310', '0x7ffc4ffbb2f0']
[-1, -2]
We can see that the python list slice creates a copy. Once the new array g is modified then the elements of the new array change ids.
If we repeat the same with numpy arrays
# numpy arrays
import numpy as np
vc = np.array([1,2,3,4])
print('original array')
print(hex(id(vc)))
print([hex(id(vc[i])) for i in range(len(vc))])
print(vc)
# --
g = vc[1:3]
print('array slice')
print(hex(id(g)))
print([hex(id(g[i])) for i in range(len(g))])
print(g)
# --
g[:] = [-1,-2]
print('original array')
print(hex(id(vc)))
print([hex(id(vc[i])) for i in range(len(vc))])
print(vc)
# --
print('array slice')
print(hex(id(g)))
print([hex(id(g[i])) for i in range(len(g))])
print(g)
we get the output
original array
0x211acbe64e0
['0x211acd107e0', '0x211acd107e0', '0x211acd107e0', '0x211acd107e0']
[1 2 3 4]
array slice
0x211acd674e0
['0x211acd107e0', '0x211acd107e0']
[2 3]
original array
0x211acbe64e0
['0x211acd107e0', '0x211acd107e0', '0x211acd107e0', '0x211acd107e0']
[ 1 -1 -2 4]
array slice
0x211acd674e0
['0x211acd107e0', '0x211acd107e0']
[-1 -2]
We see that slicing of numpy arrays produces views, but the element ids make no sense. I was thinking of using ids as a means to understand when things are copied with numpy (and with pandas) and when views are created but I cannot understand what is going on.