[Tutor] Cython vs Python-C API

Stefan Behnel stefan_ml at behnel.de
Tue Nov 15 09:09:05 CET 2011


Jaidev Deshpande, 14.11.2011 21:30:
> I need to perform cubic spline interpolation over a range of points, and I
> have written the code for the same in C and in Python.
>
> The interpolation is part of a bigger project. I want to front end for the
> project to be Python. Ideally I want Python only to deal with data
> visualization and i/o, and I'll leave the computationally expensive part of
> the project to C extensions, which can be imported as functions into Python.
>
> To this end, the interpolation can be handled in two ways:
>
> 1. I can either compile the C code into a module using the Python-C/C++
> API, through which I can simple 'import' the required function.
> 2. I can use the Python code and extend it using Cython.

3. use the existing C code and wrap it with Cython, likely using NumPy to 
pass the data, I guess.

Why write yet another version of your code?

I would strongly suggest not to put C-API calls into your C code. It will 
just make it less versatile (e.g. no longer usable outside of CPython) and 
generally harder to maintain.

Stefan



More information about the Tutor mailing list