python bindings, how does it work?

ashim picture ashim · Apr 18, 2012 · Viewed 45.2k times · Source

I am exploring python. I curious about python bindings. Could anybody explain, how it is possible that you can have access to C libraries from Python.

Answer

yak picture yak · Apr 18, 2012

There are several ways to call code written in C from Python.

First, there is the ctypes module in the standard library. It allows you to load a dynamic-link library (DLL on Windows, shared libraries .so on Linux) and call functions from these libraries, directly from Python. Such libraries are usually written in C.

Second, in case of CPython there is the Python/C API. It can be used in two major ways:

A dynamic-link library can be written in C in such a way that CPython will treat it as a module (you will be able to import it in your Python programs). The Python/C API allows the library to define functions that are written in C but still callable from Python. The API is very poweful and provides functions to manipulate all Python data types and access hte internals of the interpreter.

The second way to use the C API is to embed Python in a program written in C. The C program is then able to create built-in modules written in C and expose C functions, much like in the dynamic-link library approach. The API also allows the program to execute scripts which can then import and use the built-in modules. This can be used to create a Python based plug-in system.

"Bindings" are implemented either as a pure Python library using ctypes or as a dynamic-link library using Python/C API. The second option is sometimes used with tools like SWIG which make the task easier by taking care of generating the "boiler-plate" code or Boost.Python which provides a C++ API on top of Python/C API making it easier to interface with C++ code.

Further read: Foreign Function Interface