tags:

views:

1266

answers:

2

I have an array of objects in Python

[obj1, obj2, obj3]

and I want to pass them to off to a C++ function to perform some computation. I'm using SWIG to write my interface. The class type of the passed object is already defined in C++.

What's the best way to do this?

A: 

A popular way is through Boost.Python.

Max Lybbert
+2  A: 

It depends on if your function is already written and cannot be changed, in which case you may need to check Swig docs to see if there is already a typemap from PyList to std::vector (I think there is). If not, taking PyObject* as the argument to the function and using the Python C API for manipulating lists should work fine. I haven't had any problems with it so far. For self-documentation, I recommend typedef'ing PyObject* to some kind of expected type, like "PythonList" so that the parameters have some meaning.

This may also be useful:

http://stackoverflow.com/questions/276769/how-to-expose-stdvectorint-as-a-python-list-using-swig

Michel