Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use C++ features while extending Python?

The Python manual says that you can create modules for Python in both C and C++. Can you take advantage of things like classes and templates when using C++? Wouldn't it create incompatibilities with the rest of the libraries and with the interpreter?

like image 729
Javier Avatar asked Jul 26 '09 23:07

Javier


People also ask

Can Python and C work together?

Extending Python with C or C++ It is quite easy to add new built-in modules to Python, if you know how to program in C. Such extension modules can do two things that can't be done directly in Python: they can implement new built-in object types, and they can call C library functions and system calls.

What are C extensions Python?

A CPython extension module is a module which can be imported and used from within Python which is written in another language. Extension modules are almost always written in C, and sometimes in C++, because CPython provides an API for working with Python objects targeted at C.

Is include in C program same as import in Python?

#include in C and C++ is a textual include. import in Python is very different -- no textual inclusion at all! Rather, Python's import lets you access names exported by a self-contained, separately implemented module.


2 Answers

It doesn't matter whether your implementation of the hook functions is implemented in C or in C++. In fact, I've already seen some Python extensions which make active use of C++ templates and even the Boost library. No problem. :-)

like image 82
vog Avatar answered Sep 25 '22 17:09

vog


The boost folks have a nice automated way to do the wrapping of C++ code for use by python.

It is called: Boost.Python

It deals with some of the constructs of C++ better than SWIG, particularly template metaprogramming.

like image 30
John Mulder Avatar answered Sep 23 '22 17:09

John Mulder