Per definition, id function will return address of the object in memory for CPython.
Based on @hpaulj comment, if object is a non-native Python object, id() will return meaningless result.
It doesn't make sense to call id function on non-python object of ndarray.
That's why id of ndarray elements looks wired.
Return the “identity” of an object. This is an integer (or long integer) which is
guaranteed to be unique and constant for this object during its lifetime.
Two objects with non-overlapping lifetimes may have the same id() value.
CPython implementation detail: This is the address of the object in memory.
The result of invoking id() on native python array elements works as expected and return continuous memory addresses of array elements as below:
import array
pa = array.array('l', [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
print(pa)
for i in range(16):
print(pa[i],"=>",hex(id(pa[i])))
array('l', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
0 => 0x7ffe63d01680
1 => 0x7ffe63d016a0
2 => 0x7ffe63d016c0
3 => 0x7ffe63d016e0
4 => 0x7ffe63d01700
5 => 0x7ffe63d01720
6 => 0x7ffe63d01740
7 => 0x7ffe63d01760
8 => 0x7ffe63d01780
9 => 0x7ffe63d017a0
10 => 0x7ffe63d017c0
11 => 0x7ffe63d017e0
12 => 0x7ffe63d01800
13 => 0x7ffe63d01820
14 => 0x7ffe63d01840
15 => 0x7ffe63d01860
Call id on ndarray element which is non-native python object returns meaningless result as below(Eg:aa[0],aa[2],aa[4] return same id):
import platform
print(platform.python_implementation())
aa = np.arange(16)
print(aa)
for i in range(16):
print(aa[i],"=>",hex(id(aa[i])))
CPython
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
0 => 0x23d4913ba70
1 => 0x23d4913ba90
2 => 0x23d4913ba70
3 => 0x23d4913ba90
4 => 0x23d4913ba70
5 => 0x23d4913ba90
6 => 0x23d4913ba90
7 => 0x23d4913b570
8 => 0x23d4913b570
9 => 0x23d4913b710
10 => 0x23d4913b570
11 => 0x23d4913b710
12 => 0x23d4913b570
13 => 0x23d4913b710
14 => 0x23d4913b570
15 => 0x23d4913b550