How to determine if a PyObject is a numpy PyArrayObject

Viewed 887

I'm new to the Python C-API.

Currently I get objects from the embedded Python module via

PyObject* a = (PyObject*) PyObject_GetAttrString(pModule, "a");
std::cout << "a as long is " << PyLong_AsLong(a) << std::endl;

I access numpy objects via

PyArrayObject* array = (PyArrayObject*) PyObject_GetAttrString(pModule, "A");

How can I test if the object is a really a PyArrayObject? In other words how to do what I would do in Python via isinstance(a, numpy.ndarray)?

1 Answers

Use PyArray_Check or PyArray_CheckExact. Use PyArray_Check if subclasses are okay, or PyArray_CheckExact if you need an object with type exactly numpy.ndarray.

Related