Summary
In this chapter, you learned about writing and using extensions in C/C++. As a quick recap, we covered:
- Loading external (system) libraries such as
stdlib
usingctypes
- Creating and handling complex data structures using
ctypes
andCFFI
- Handling arrays using
ctypes
andCFFI
- Combining C and Python functions
- Important caveats regarding numeric types, arrays, overflows, and other error handling
Even though you can now create C/C++ extensions, I still recommend that you avoid them, if possible, because it is so easy to end up with bugs. Even the code examples in this chapter don’t handle many of the possible error scenarios and, as opposed to errors in Python, if these errors happen in C, they can kill your interpreter or application entirely.
If your goal is better performance, then I would recommend trying numba
or cython
instead. If you really need interoperability with non-Python libraries, however, these libraries are...