Python and C

Alex Martelli aleaxit at yahoo.com
Fri Mar 10 16:46:08 CET 2006


<diffuser78 at gmail.com> wrote:

> I was a C Programmer for a while. Lately started to learn Python for
> one small project at school. I joined a small company where they use
> C++ for development.
> 
> Can we use Python and C together ? I mean create some classes in Python
> and some number crunching algorithms coded in C (for speed) and
> integreate both of them.

Sure!  It's extremely common practice, known as "extending Python".


> Could somebody just show a real small example or give pointers to the
> same.

Besides the Extending/Embedding and C API documents which are part of
the standard set of Python docs, to which you've already been pointed in
other responses, you can find tutorials on the net. A tiny one is at
<http://www.developer.com/lang/other/article.php/2191421>, with pointers
to other materials. Also, examples can be found in the Demos directory
of the Python source distribution -- even if you have a binary distro
installed, get the source distro anyway, it's full of goodies and you
can use much of Python itself as an example of Extending (the Modules
and Objects directories of the source distro, in particular, are just
such examples).

C is the lowest, most fundamental level of extension, but there are many
other alternatives -- SWIG to wrap existing libraries, Boost or SCXX or
SIP to wrap specifically C++ with very different philosophies (template
heavy, minimal, Qt-based), pyrex (a Python "dialect" plus C-like
declarations to make it compilable to fast machine code), and others
yet.


Alex



More information about the Python-list mailing list