[Numpy-discussion] Best way to insert C code in numpy code
Tue Sep 22 09:09:23 CDT 2009
René Dudfield skrev:
> Another way is to make your C function then load it with ctypes(or
> wrap it with something else) and pass it pointers with
numpy.ctypeslib.ndpointer is preferred when using ndarrays with ctypes.
> You can find the shape of the array in python, and
> pass it to your C function. The benefit is it's just C code, and you
> can avoid the GIL too if you want. Then if you keep your C code
> separate from python stuff other people can use your C code in other
> languages more easily.
You can do this with Cython as well, just use Cython for the glue code.
The important difference is this: Cython is a language for writing C
extensions, ctypes is a module for calling DLLs.
One important advantage of Cython is deterministic clean-up code. If you
put a __dealloc__ method in a "cdef class", it will be called on garbage
Another nice way of interfacing C with numpy is f2py. It also works with
C, not just Fortran.
Yet another way (Windows specific) is to use win32com.client and pass
array.ctype.data. That is nice if you have an ActiveX control; for
Windows you often get commercial libraries like that. Also if you have
.NET or Java objects, you can easily expose them to COM.
More information about the NumPy-Discussion