Python, C++ interaction

Michael Kreim michael at perfect-kreim.de
Wed Dec 3 03:29:22 EST 2014


Hi,

we are working on a small scientific program that helps us in developing 
and testing of new numerical methods for a certain type of biochemical 
problems. I spare you the math ;-)

We code our new methods in Python and compare them with the existing 
methods. Unfortunately, these existing methods are quite slow and need a 
lot of runtime. Therefor we implemented them in C++.

Now, we like to combine these two approaches. So we can develop a new 
method in Python and compare it easily with the results from the C++ 
functions.

I did some googleing on extending Python by C++ code but I did not find 
something that satisfies me. I gave SWIG a try, but several webpages 
disadvised me of using it. Also my small experiments did not work. Now, 
I read about ctypes. But as far as I understood it, I have to write a C 
wrapper for my C++ code and then a Python wrapper for my C code. That 
seems a little complicated.

My C++ code is structured as a class that contains several variables and 
functions. Then everything is compiled as a shared library. I can write 
things like:

MyClass.initialvalue = 5;
MyClass.timestep = 0.1;
...
MyClass.solve();

I would like to keep this structure in python, but if I understand 
ctypes correctly I would loose the class approach.

So after the long writeup I come to my questions:

What are you using to wrap C++ classes for Python?
Can you recommend swig? Should I give it another try?
Did I misunderstood ctypes?

Also later on in our code development we would like to pass python 
lambda functions to C++. So far I understood that this seems not to be 
possible. Do you have any ideas or hints on this topics?

Thank you very much.

Cheers,

Michael



More information about the Python-list mailing list