4

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?

1
  • "best way" given what constraints? Are external libraries allowed, or do you want to go through Python's special purpose facilities? Commented Dec 15, 2008 at 20:06

1 Answer 1

2

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:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.