Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python bindings, how does it work?

Tags:

python

c

binding

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.

like image 622
ashim Avatar asked Apr 18 '12 03:04

ashim


People also ask

How do Python bindings work?

Cython. The approach Cython takes to creating Python bindings uses a Python-like language to define the bindings and then generates C or C++ code that can be compiled into the module. There are several methods for building Python bindings with Cython . The most common one is to use setup from distutils .

How do I enable bindings in Python?

Since ns 3.11, the modular bindings are being added, in parallel to the old monolithic bindings. They are disabled by default. To enable, use the --bindings-type waf option (e.g. ./waf configure --bindings-type=modular).

Can you use C++ and Python together?

Python Library is a open source framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools, just your C++ compiler.

How much faster is C++ than Python?

Depending on the complexity of calculations, C++ is anywhere from 10 to 100 times faster than Python. Python programs also tend to use more RAM than applications built with C++. However, many programmers acknowledge that the simple syntax of Python makes it a much faster language for development.


2 Answers

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 powerful and provides functions to manipulate all Python data types and access the 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

like image 60
yak Avatar answered Sep 25 '22 08:09

yak


Answer is simple, python (CPython) interpreter is written in C and it can call other C libraries dynamically, your C extension module or embedded C code can be easily called from any other C code.

CPython allows special hooks so that it can call other C code or can be called from other C code. It need not even be C, any language which compiles to native code and have same calling convention.

For a simple case consider you create a program called mython, which can load any shared library and tries to call a function run e.g.

lib = dlopen("mylib.so", RTLD_LAZY); func = dlsym(lib, "run"); (*func)(); 

So in way you have loaded a module and called its code, CPython does that but in more complex way, providing better interfaces and objects to pass around, plus there are other intricacies involved of memory management, thread management etc.

So platform of Python implementation must match to language in which it is being extended, e.g. CPython is not extensible in Java but Java implementation of Python called Jython can be extended in Java and similarly .NET implementation IronPython can be extended in .Net languages.

like image 32
Anurag Uniyal Avatar answered Sep 23 '22 08:09

Anurag Uniyal