Passing Python array to C ++ vector using Swig

I have an array of objects in Python

[obj1, obj2, obj3] 

and I want to pass them to C ++ functions to do some calculations. I use SWIG to write an interface. The class type of the passed object is already defined in C ++.

What is the best way to do this?

+3
source share
1 answer

It depends on whether your function has already been written and cannot be changed, in which case you may need to check your Swig docs to see if there is already a typical map from PyList to std :: vector (I think it is) . If not, using PyObject * as a function argument and using the Python C API to manage lists should work fine. So far I have not had any problems. For do-it-yourself documentation, I recommend typedef'ing PyObject * for some expected type, like "PythonList", so that the parameters make some sense.

It may also be useful:

How to set std :: vector <int> as a Python list using SWIG?

+2
source

All Articles