#StackBounty: #c++ #boost #boost-python How to create a numpy array of boost::python::object types

Bounty: 200

I’m trying to create a 2×2 numpy array of python objects:

#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
int main()
{
    Py_Initialize();
    boost::python::numpy::initialize();
    boost::python::tuple shape = boost::python::make_tuple(2, 2);
    boost::python::object obj;
    boost::python::numpy::dtype dt = boost::python::numpy::dtype(obj);
    boost::python::numpy::ndarray array = boost::python::numpy::empty(shape, dt);
    std::cout << "Datatype is: " << boost::python::extract<char const *> boost::python::str(array.get_dtype())) << std::endl;
}

But the output is "Datatype is: float64" rather than a python object type.

What am I doing wrong?

I suspect I’m misusing the dtype constructor.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.