std::vector to boost::python::list

后端 未结 6 674
星月不相逢
星月不相逢 2020-11-27 02:28

I have a method in c++ that gets called from python and needs to return a python list object.

I have already created the method, and its attached to an exposed class

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 03:08

    If you only want to create python list manually (and have the function return py::list rather than vector), do it like this:

    /* using namespace std; namespace py=boost::python;
       #define FOREACH BOOST_FOREACH
    */
    vector ss;
    py::list ret;
    FOREACH(const string& s, ss) ret.append(s);
    return s;
    

    For automatic conversions, define the converter for vector from python list to c++ and from c++ to python list -- I just wrote about that at Instantiating shared_ptr's in boost::python (the second part of the reply); that way, you get realy python lists.

    Another possibility for automatic conversion (which I have no experience with) is to use indexing_suite, which will wrap vector as a special class in python, as a colleague mentioned here already.

提交回复
热议问题