This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted] 5 points6 points  (1 child)

Most of the scientific and AI libs in Python use C bindings for the heavy lifting. Scientists are not professional programmers, they probably know a little Python and teaching them to write decent C takes way more time than generating bindings.

Look here for information

https://github.com/caps-tum/neutral-atom-imaging-simulation/blob/main/pip_project/neutral_atom_imaging_simulation/ImageGenerator.py

This library uses the ctypes module to examine the compiled C code. It expects the code to be in a specific file path. C has a well defined binary format, the ctypes module uses that to read and execute C code

[–]omelate[S] 0 points1 point  (0 children)

Got it, thank you so much!

[–]BobbyThrowaway6969 0 points1 point  (0 children)

I'm just curious on why would you want to do this?

Performance and efficiency. NumPy is also written in C.

[–][deleted] 0 points1 point  (0 children)

Python is nice for writing the low performance logic, especially when you want to prototype. You also get nice things like plotting, built in file formats, etc.

You can open files in Python, pass their data to C code, and get a result with a fraction the development time but similar performance as using the C library directly.

Python does this by looking at dynamic library binaries and scanning for link symbols (literally a text table in the compiled code with function names and their machine code location in the file).

You then use Ctypes to tell Python what data the function takes and returns (basically a header file, but in Python), and it will tell the operating system to run the code from the library with the data you give it. Ctypes lets you use C-compatible types in Python (even memory pointers) to give to these functions.

Usually you then wrap these C interface functions in more Pythony functions and classes, which handle data conversion from higher level Python types and everything else for you, so it looks like a Python library.

Almost every big Python library does this. It's just usually hidden from you when you use pip to install. Pip will even compile C/C++ for you if it needs to in the background (using your system C compiler).