Introduction
Wrapping C++ libraries for use from Python is
a profitable undertaking, given the wealth of functionality that
becomes available after doing so. Python, in contrast with Java,
has been designed from the outset to be easily extensible with
‘foreign' libraries written in C. C++, however, is a far more
complex language than C, and requires careful attention to the
creation, ownership and destruction of objects. Trolltech have
made the work even more complicated with the invention of a
special meta-object pre-compiler needed to get techniques like
signals and slots working.
These days it is not usual to wrap C++
libraries by hand. If only because of the size of the Qt
libraries, it probably isn't practical to write these bindings
manually, so an automated tool, sip, was
developed by Phil Thompson (the PyQt developer) to generate the
necessary binding code.
As you know, PyQt is a set of Python
bindings for the Qt libraries. That means that PyQt isn't a
translation of Qt into Python—instead, the ‘bindings' let
you access and use the C++ Qt libraries from the Python language
via an intermediate wrapper library, which is also written (or
rather, generated) in C++.
The sip program
generates these C++ ‘wrappers'. Wrappers are chunks of C++ code
that allow Python to pass data to and from other C++ code and to
invoke C++ methods or functions. Sip gets
its name (and some of it's architecture) from another wrapper
generator named swig. In fact,
sip started out as a small
swig, although it has grown a bit since
then. It is specifically designed to generate Python bindings
for C++ libraries, while swig is more
general purpose, and can wrap C libraries for a variety of
scripting languages. Of course, sip also
offers some additional functionality, like support for signals
and slots, Python inheritance of C++ objects, and many other C++
language features:
Re-implementation of C++ virtual methods
in Python.
Sub-classing C++ classes from
Python.
Access to a C++ class's protected
methods.
Overloading of C++ functions and methods
with different parameter type signatures.
Automatic translation between C++
classes and similar (but more appropriate) Python
types.