How does Python call C?

Question:

How exactly can Python call a C library? Tensorflow, for example, I believe is written mostly in C, but can be used from Python. I’m thinking of implementing something like this in my own (interpreted) programming language (written in Go, but I assume it would be a similar process).

What happens when a Python program calls a C function? I’m thinking either RPC or DLLs, but both of them seem unlikely.

Asked By: Zac

||

Answers:

cPython has two main ways to call C code: either by loading a shared library and calling its symbols, or by packing C code as Python binary modules and then calling them from Python code as though they were ordinary Python modules, which is how high performance stuff in the standard library is implemented – e.g. json.

Loading a shared library and calling functions from it using the ctypes module is rather trivial, and you can find a lot of examples here: https://docs.python.org/3/library/ctypes.html

Packing your C code as binary Python module requires a lot of boilerplate and careful attention to details such as ref counting, null pointers, etc, and is documented here: https://docs.python.org/2/extending/extending.html

There are several libraries that automate the process and generate binding code for you. One example is boost.python: https://www.boost.org/doc/libs/1_65_0/libs/python/doc/html/tutorial/index.html

Answered By: Not_a_Golfer
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.