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.
I am exploring python. I curious about python bindings. Could anybody explain, how it
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
.soon Linux) and call functions from these libraries, directly from Python. Such libraries are usually written in C.Second, in case of
CPythonthere 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
CPythonwill 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
ctypesor 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