C++ and Python

Alex Martelli aleaxit at yahoo.com
Thu Nov 9 13:23:22 CET 2000


"Michael Ströder" <michael at stroeder.com> wrote in message
news:3A0A5AE1.41872D31 at stroeder.com...
> Hans Brand wrote:
> >
> > Check out www.swig.org
>
> You might also want to check out Python-SIP developed by the
> PyQt/PyKDE people.
>
> "description:
> SIP is a tool to generate C++ interface code for Python. It is
> similar
> to SWIG, but uses a different interface format. It was used to build
> PyQt and PyKDE, and has support for the Qt signal/slot mechanism."

My personal favourite tool for this task is David Abrahams'
py_cpp, see:
http://people.ne.mediaone.net/abrahams/downloads/py_cpp.html

It focuses on: using your C++'s compiler built-in mechanisms
(particularly templates), rather than re-inventing the needed
parsing, type-mechanisms, etc; wrapping (non-invasively) some
existing C++ classes and functions (e.g. a 3rd party library);
exposing to Python code Python-inheritable 'extension classes',
including the ability to override C++ virtual functions with
Python code _even for functions that are "called back" by C++
code in the framework you're wrapping_; letting you write C++
code 'as C++ code is ordinarily written', e.g., relying on
compiler-performed resolution of overloading, based on types
of arguments being passed, rather than receiving 'generically
typed' parameters and having to arrange dispatching based on
their types yourself.

It's still pretty young, and doesn't support _everything_ one
would wish (yet): keyword-parameters are not currently there;
some (numerics-oriented) special-methods such as __radd__ and
__iadd__ are not supported yet (though they're promised for
the future); __complex__ apparently *just can't* be supported
by any way of writing extensions (in C, or C++, or whatever),
today, because the Python interpreter handles it in a peculiar
and different way from other special-methods.

But it has proven quite solid and reliable for me with the
noted (and documented) exceptions.  The fact that it uses
your own compiler's template-mechanics worried me a bit at
first, but in fact it runs just fine with Visual C++ 6 (SP4)
as well as gcc.  I was apparently the first to try it out
on Python 2 (rather than 1.5.2), but (interestingly...:-)
that gave no problems either.


Alex






More information about the Python-list mailing list