[Tutor] Using C in python
alan.gauld at btinternet.com
Thu Mar 26 01:13:30 CET 2009
"amit sethi" <amit.pureenergy at gmail.com> wrote
> what are the ways in which i can use C in python programs .
You can't use C directly what you can do is access libraries written in C.
In fact quite a few of the standard libraries are written that way.
So it depends what you want to do.
If you have C source code that you want to execute from Python
you can either::
a) Build a library and create a Python wrapper so it looks like a module
b) Build an executable file and call that using subprocess/popen etc
c) If its one of the common C libraries or a Windows DLL you can
probably use an existing framework to call it. For example ctypes
will access a lot of stuff.
If you want to execute Python code from a C program, you can
do that too, but it doesn't sound like thats what you need?
> SWIG bindings are there any other . Also Could anyone explain how Swig
> bindings work?
SWIG bindings just expose C code in the right format for Python
to see the code as a Python module. Its slightly easier than doing
it by hand in C because it automates what is a pretty brain
dead repetitive process. ( I've only used SWIG with Tcl but I
assume its a similar process for Python.)
Author of the Learn to Program web site
More information about the Tutor