Providing a Python wrapper to a C++ type.

Evan Driscoll driscoll at cs.wisc.edu
Tue Oct 16 12:42:44 EDT 2012


[Stefan gave part of an answer here, but I've got an addition too.]

On 10/16/2012 6:46 AM, Marco Nawijn wrote:
> I never worked with Cython (but I know it is very powerful and interesting) but in my mind there are slight differences in usage scenario between e.g. Boost Python and Cython. For me the idea of Cython is that your main code is in Python, but you want to improve the performance of specific parts of the code. In that case, Cython is the way to go. In case of Boost Python, the scenario for me is that you have a main program/library in C++, but you want to be able use the functionality from Python. 
> 
> Do you agree with this view?

Sort of, but sort of not. I've only used Cython a little bit, but it
seems to work equally well if you want to write a C module for some
reason (e.g. to interface with existing C code) but don't want to deal
with the standard CPython C API directly.

For instance, I used it to wrap the OS's opendir/readdir
(FindFirstFile/FindNextFile) functions: I just wrote a bit of code using
Cython's extensions, and I get a module I can import and use as normal.
I certainly didn't do it for speed, I did it because it seemed like a
reasonable way to get access to those APIs. (ctypes was insufficient for
my needs.)

So wrapping a C++ class using Cython also seems pretty natural to me,
assuming that Cython does OK with C++ and isn't restricted to C (which I
have no idea about).

Evan



More information about the Python-list mailing list