[Newbie] embed c++ in Python

Chris Barker Chris.Barker at noaa.gov
Tue Apr 2 11:29:24 EST 2002


> > > I want to know how to write a function which receives some variables as
> > > argument changes them and sends the results back to Python.

This is a very straightforward oridinary extension problem. Look on the
web for "Extending Python C". The Python API is C so even if you use
C++, you are pretty much just writing C. The exception to this is the
Boost library, which looks very nice. It wraps the C Python objects in
C++. If you have one or two simple extensions to make, I'm not sure it's
worht learning Boost, but ifyou are doing alot it probably is. For
simple C extensions, start with the standard docs: the extending and
embedding tutorial, and then the C API reference.

Note: Python Integers are imutable, so it may not be possible to do
exactly what you specified. It is very easy to pass a mutable python
object (List, dictionary) to a C extension and change it there. You
could also easily write one that you would call this way:

(a,b) = Cfunction(a,b)

Look through teh docs, you'll figure it out.

Another source I used for learning to write extensions is the NumPy
docs, there are some examples there that are more involved than the
examples in the standard docs. Even if you have no use for NumPy arrays,
the examples might be usefull.

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer
                                    		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the Python-list mailing list