Combining Python and C++07/19/2001
Python and C++ easily complement each other. Python gives you rapid development and flexibility, C++ gives you speed and industrial strength tools. While there is no standard tool for extending Python with C++, there are many Python wrappers to C++ libraries, particularly GUI toolkits. the developers of these interfaces haven't just given us the wrappings, they have given us the wrappers as well, tools to give any C++ object a Python interface.
Sip and SWIG are used to automatically generate interfaces without having to write a lot of helping code. SWIG is primarily used with C code. Sip is a derivative of SWIG that focuses on C++ code. SIP was developed by the Kompany to assist them in creating python interfaces to the Qt GUI toolkit and the K Desktop Environment. While automatic generation sounds nice, it isn't completely automatic. You still end up having to write code for more complex programs and tweak your results. For some tasks creating an extension by hand is preferable.
Paul DeBois's CXX makes it easier to write C++ extension for Python. It is essentially a C++ version of Python's C API. CXX handles reference counters and converts exceptions. It eliminates the bulk of the type checking and cleanup that clutters most C++ extensions. This helps you focus on the important parts of the code you are writing. It allows you to use C++ Standard Template Library containers as Python lists and tuples. Perhaps it was this that inspired Tom Malcolmson to create PySTL exposing the Standard Template Library to Python.
Using a modified version of CXX Malcolmson has begun work on making STL containers and algorithms easily accessable to Python. While much of the STL is redundant with features of Python itself, it is faster. Using PySTL you could speed up certain functions, trading some portability for native speed. You could also use PySTL educationally, to explore the STL interactively from Python. The project is in its infancy and support for Linux and Unix is minimal, Malcolmson is a Windows developer. He provides source code including a configure script and makefiles for Unix systems, but I was unable to get it to compile on a RedHat 7.1 system. There are precompiled binaries available for Windows users.
The educational aspect of PySTL intrigued me enough that I went looking for more STL information. I found a good STL tutorial on the late Jak Kirman's web pages. Python programmers may recognize him for his Perl/Python Phrasebook. Kirman died suddenly late last year. I don't know how much longer Brown University will continue to host his pages, so look them over while you still can.
One final tool I would like to mention is David Abrahams' Boost.Python. With Boost.Python you write a small bit of helping code to create a shared library your Python program can import and use. Like Sip it is designed to take advantage of existing code as well as writing new extensions. Like CXX it handles references, callbacks, typechecking and cleanup for you. Writing interfaces for long pieces of code might be tedious, but the process is simple and you have greater control than using an automated wrapping tool. Abrahams has written thorough documentation for Boost.Python, including a comparison page that lists even more tools I haven't covered here.
With these tools, there is no reason to choose between Python and C++. You can start with one and use it as a launching point for learning the other. If you know both, you can take advantage of the strengths of both, achieving more faster with a hybrid project than you could with either alone.
Stephen Figgins administrates Linux servers for Sunflower Broadband, a cable company.
Read more Python News columns.
Return to the PHP DevCenter.