Using SWIG to bind C++ to Python

An increasingly popular approach to scientific computing is to combine Python and compiled modules. Such an approach merges the high performance typically found in compiled routines with the interface of a flexible, scalable, and easy-to-learn interpreted language. Although using C to hand-code extensions to Python binds the latter to a given compiled asset in C++, programmers who used C++'s more advanced features (until recently) lacked the automated support available in Fortran and C. One tool for creating Python bindings to C the Simplified Wrapper and Interface Generator. SWIG-an open-source application used by a large and ever-expanding community-began as an effort to expose physics packages in a large parallel simulation code to interpreted languages. SWIG preprocesses C and C++ code and generates library bindings in several interpreted languages including Python, Pert, Tcl, and Java. Recent improvements to SWIG provide greater support for binding C++ code. SWIG now creates, for example, bindings for some of C++'s more advanced features such as templates and exceptions. This article explores how SWIG does this by examining a series of small C++ code examples.