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.
- Is Python call by reference or call by value
- How to Call a C function in Python
- Python - Call function from another function
- call() decorator in Python
- Python: Call Parent class method
- Python - Call function from another file
- Decorator to print Function call details in Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.