C Python extension to export an Function

Christian Gollwitzer auriocus at gmx.de
Thu Sep 1 09:12:02 EDT 2016


Am 01.09.16 um 14:30 schrieb Ganesh Pal:
> On Thu, Sep 1, 2016 at 12:32 PM, dieter <dieter at handshake.de> wrote:
>
>> Ganesh Pal <ganesh1pal at gmail.com> writes:
>>
>>> Iam pretty new to C Python extension , I was able to export few simple
>>> modules to python and it look like the cool thing to do ...
>>
>> Maybe, it is a good idea to have a look at "cython".
>>
>> "cython" is a compiler. It translates Python code enhanced with
>> special annotations into C. The annotations mostly tell the compiler
>> that something ("object", "method", "function", ...) should be at "C"
>> rather than "Python" level, thus avoiding much of Python's overhead
>> and allows to do things possible in "C" but not in "Python".
>>
>>
>> Developing safe "C" extensions for Python is difficult. You
>> need some quite deep understanding of the Python-C interface
>> and must be very careful to observe all requirements (especially
>> those related to proper reference management).
>>
>> Developing "C" extensions with "cython" is much easier as
>> "cython" hides many of the complexities and takes care of most
>> requirements.
>>
>
> Dear  Dieter ,
>
> Really appreciate the reply and your suggestion on  trying to use "cython"
> ,  but my whole idea of using  "C" extension is to regular C codes .  We
> have bunch of C code that's already available and   C -Python seems to suit
> me better

Yet another option is to try SWIG. SWIG is a special compiler, you feed 
C header files which declare your functions, and it writes the wrapper 
code for you. In simple cases (standa data types like int, float, 
string,...), it might be enough. In more advanced cases, you can add 
your wrappers as "typemaps".

	Christian




More information about the Python-list mailing list