ctypes.py_object_Array is not a class you can import. It is the naming pattern ctypes uses for arrays of ctypes.py_object: when you write ctypes.py_object * 4, ctypes creates a new fixed-size array type named py_object_Array_4. Each slot holds a reference to a Python object, starts out as NULL, and is raised as ValueError if you read it before assigning.
This article explains how to create these arrays, what they do with references, and why a plain Python list is usually the better choice.
Multiplying any ctypes type by an integer creates an array type of that length. The array class is cached, so ctypes.py_object * 3 always returns the same class.
_length_ and _type_ describe the array, and sizeof is the length times the size of one pointer, which is 8 bytes on 64-bit platforms. Searching for a ctypes.py_object_Array attribute fails because the type only exists once you build it.
A new instance has every slot set to NULL. Reading an empty slot raises an error, so assign before you read.
Slicing returns an ordinary Python list, and the array supports len, iteration and index assignment. It does not support append, pop or resizing, because the length is part of the type.
You can pass the initial values to the constructor. If you pass fewer than the array length, the remaining slots stay NULL, and passing more raises IndexError.
Storing an object in a py_object array increases its reference count, and ctypes also records the object in the array's private _objects dictionary, so the object stays alive as long as the array does.
When the array is deleted, the references are released. Be careful with the reverse direction: if you pass the array's address to C code that stores PyObject * pointers, C must follow the normal reference counting rules itself, or you must keep the array alive for as long as C uses it.
An array of py_object is laid out in memory as a C array of PyObject *. A function with the signature void f(PyObject **items, size_t n) can therefore receive it directly, but only if the call keeps the GIL held and the function respects the Python C API rules. Use ctypes.PyDLL (or ctypes.pythonapi for built-in functions) for that, not ctypes.CDLL, which releases the GIL while the function runs.
This is an advanced pattern. Writing a normal extension module with the C API, Cython or cffi is safer, and a py_object array gives no protection against wrong types or NULL entries.
| Need | Use |
|---|---|
| Grow, shrink, sort or mix types freely | list |
Fixed array of PyObject * for a C function that takes PyObject ** | ctypes.py_object * n |
| Array of numbers for C | ctypes.c_int * n, array.array or NumPy |
| Pass many numbers to C efficiently | memoryview, array.array or NumPy |
Most Python code that looks like it needs a py_object array only needs a list, because py_object arrays do not make anything faster; they exist for interoperability with the C API.
No. It is a generated class name such as py_object_Array_4. Create the type with ctypes.py_object * 4 and keep it in your own variable.
You read an element that was never assigned. New py_object arrays contain NULL pointers, so assign every slot before reading it, or initialise the array with values.
No, its length is fixed by the type. Build a new array type of the length you need, or use a Python list.
No. It stores the same object pointers and adds ctypes overhead on each access. Use it only when a C function requires a PyObject **.
py_object_Array_N is the type you get from ctypes.py_object * N: a fixed-size array of Python object references that starts out NULL, keeps its elements alive and slices into a list. Use it when C needs a PyObject **, call such functions through PyDLL so the GIL stays held, and use a plain list for everything else.