Reputation: 291
I have a device which can be controlled through a C++ class (https://github.com/stanleyseow/RF24/tree/master/RPi/RF24).
I'd like to be able to use this class in Python, and thought I could wrap it.
I found many ways to do it, but not much detailed documentation with examples. In particular, I found Boost, Cython, SWIG and the native C Python API.
Which one is the best method in which case ? And do you have some links to detailed documentations / examples about this ?
Thanks !
Upvotes: 1
Views: 487
Reputation: 153899
There is no "best"; it depends entirely on your circumstances.
For a single class, the native C Python API isn't too difficult, but you do have to create an entire module, then the class. It would be simpler if you exposed a procedural interface, rather than a class. If you only have one instance of the device, this would be an appropriate solution.
SWIG is very good for taking C++ class definitions and generating a Python module which contains them. The resulting code is relatively complex, since SWIG tries to cover all possible versions of Python; for anything 2.7 or later (and perhaps a little earlier), you can do everything directly in C++, without any intermediate Python.
Boost makes extensive use of templates. This isn't really an appropriate solution for the problem; it adds a lot of complexity for something that is relatively simple if done with external tools, rather than metaprogramming. Still, if the underlying complexity doesn't scare you, it might not be that hard to use.
I'm not familiar with Cython.
Globally, if all you have is one instance of one simple class, using the native C API is probably no more difficult than the other solutions, and introduces a minimum of added internal complexity.
Upvotes: 1