By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 2,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

What's the easiest way to call a C/C++ function involving arrays from Python?

P: 1
Hi
I have some C++ code that I need to access from Python. I don't need to access any classes or anything, just a couple of functions that take one array as input and produce another array as output. The sizes of the arrays are known beforehand if that's any help.

So the functions are of the form
Expand|Select|Wrap|Line Numbers
  1. double *foo(double *in, int size);
Within Python, the arrays could be lists, tuples or NumPy arrays - it doesn't really matter to me.

I've looked at SWIG and managed to create some wrappers for simple test functions, but handling arrays turned out to be more difficult. I've succeeded in passing arrays to the functions (both using carrays.i and numpy.i typedefs), but I can't figure out how to pass arrays as output.

So now I'm asking, what would be the quickest, easiest way to get this to work? Is SWIG the way to go, or should I look at Cython or Pyrex? If someone could provide or point me to some working example code for doing this I would be forever grateful! Surely someone must have tried to do this before.
Apr 16 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.