I prepared a presentation on SWIG for our internal institute discussion group recently and ran into a couple of unexpected problems. One of the big advantages of SWIG is that it’s well-documented and there are many tutorials but still I did not find the solution to my problems instantly (i.e. using a simple Google search). That’s why I’m wrapping up my results in this blog entry for those that might have had similar problems.
Disclaimer: [SWIG](http://www.swig.org/) is only one of many interface generators to integrate your C/C++ code in a Python environment. There is a good comparison of different wrapper tools in [Python Wrapper Tools: A Performance Study](http://people.web.psi.ch/geus/talks/europython2004_geus.pdf). SWIG seems to be somewhat slower than native Python/C wrapping, SIP or Boost.Python, but still I find it much more intuitive to get a working solution quickly (if you read the complete documentation including tutorials which I obviously didn’t at that point).
In the following, I will give two examples of what can be done with SWIG. First, I will show how to integrate a [C implementation of suffix arrays](http://www.cs.dartmouth.edu/~doug/sarray/) within Python. Second, I will give an example of how to swig a C++ implementation of edit distance (aka Levenshtein distance) into Python.