2

Is there a data structure in Python(considering only built-in libraries with version 3.6x) that could meet with both of the following reqs?

1) allocate memory space sequentially, as the array structure in C provides (Or resembles C#'s LayoutKind.Sequential, Pack= 1). and

2) store custom types.(the elements in the structure would be guaranteed by design to be of the same custom type)

Things I have thought of but doesn't work:

meets 1) but fails 2): array.array

satisfies 2) but fails to meet 1): list... etc.

EDIT : As Graipher pointed out in the comment below, and the link he provides. List structure is actually an "array of pointers to Py_Objects". While that technically meets 1) that the memory is sequentially allocated, the data structure I am looking for should sequentially allocate objects, instead of object pointers.

Did I miss any handy data structure provided? Or what tricks out there could provide me with the features I want? Thanks in advance.

  • 1
    Well, a `list` does not technically fail to meet 1). It does allocate a contiguous place in memory. It is just that that place does not hold the objects, but pointers to the objects instead... – Graipher Jun 14 '18 at 15:17
  • @Graipher could you elaborate on that more? This is integral to one of the projects I am trying to do right now. My problem with the List structure is that one of the external functions expects a sequential structure(basically an c-like array) and writes in data accordingly. – nakamurayuristeph Jun 14 '18 at 15:21
  • See for example this question and links therein: https://stackoverflow.com/questions/47192150/python-list-memory-storage – Graipher Jun 14 '18 at 15:23
  • It means that while technically true it is not enough for your use case, because your code assumes that it is an array of (whatever your type is), whereas it is an array of (pointer to PyObjects). – Graipher Jun 14 '18 at 15:24
  • Firstly thanks for explaining that list actually meets 1), didn't dig into that before. And yes. the externall DLL is expecting an c-like array of a specific structure, so List, which is the structure I chose in my program, doesn't work through this case. – nakamurayuristeph Jun 14 '18 at 15:31

0 Answers0