The Python Oracle

How is the memory allocated for numpy arrays in python?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping

--

Chapters
00:00 How Is The Memory Allocated For Numpy Arrays In Python?
01:29 Accepted Answer Score 5
01:55 Thank you

--

Full question
https://stackoverflow.com/questions/5726...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #numpy #memory #mpi4py

#avk47



ACCEPTED ANSWER

Score 5


Numpy array saves its data in a memory area seperated from the object itself. As following image shows:

enter image description here

To get the address of the data you need to create views of the array and check the ctypes.data attribute which is the address of the first data element:

import numpy as np
a = np.zeros((3, 2))
print(a.ctypes.data)
print(a[0:1, 0].ctypes.data)
print(a[0:1, 1].ctypes.data)
print(a[1:2, 0].ctypes.data)
print(a[1:2, 1].ctypes.data)