In order to take advantage of the strength of both languages, developers use Python bindings which allows them to call C/C++ libraries from python.
Now, the question arises that why there is a need for doing this?
- As we know that, C has faster execution speed and to overcome the limitation of Global Interpreter Lock(GIL) in python, python bindings are helpful.
- We have a large, stable and tested library in C/C++, which will be advantageous to use.
- For performing large scale testing of the systems using Python test tools.
Let’s see the C code which we want to execute with Python :
We have to provide those cpp declarations as extern “C” because ctypes can only interact with C functions.
Now, compile this code to the shared library :
Finally, write the python wrapper:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.