Can python control complicated classes/objects written in C++

Terry Reedy tjreedy at udel.edu
Sat Jun 5 07:22:33 CEST 2004


"Bo Peng" <bpeng at rice.edu> wrote in message
news:c9rhu6$sk6$1 at joe.rice.edu...
> Dear Python group:
>
> I am planning on an application that involves several complicated C++
> classes. Basically, there will be one or two big data objects and some
> "action" objects that can act on the data. I would like to use a script
> language to control the interaction between these c++ objects.
>
> I become interested in Python since it can load C++ objects and can even
> extend C++ classes. However, I am not quite sure to what extent can
> python do this. Ideally, I would like to have something like
>
> (pseudo code, not in python)
>  > data = new TData( option1=..., option2=...)
>  > action1 = new TAction(option1=range(1,10)...)
>  > action2 = new TSubAction(option1=sin(5),..., option2=...)
>  > data.run( action1, action2)
>  > data.print

If you remove 'new' on the first three lines, the above could be Python.

> The benefits here is that I do not have to worry about user-input
> (python handles functions like range(), and numeric/string operations
> for me and send them to my objects), running logic (user create the
> scripts) and need only focus on the objects themselves. It would be
> better if users can easily extend TAction by themselves, through either
> Python or C++.
>
> I am totally new to Python. I have read boost.python, python extension
> document but still do not know exactly what to do. My questions are:
>
> 1. can Python fully read/write member data and run member functions of
> my objects?

If they are properly wrapped, yes.

> 2. can python pass complicated objects (TAction) to another object
(TData)?

Everything in Python is a first class object that can be passed, returned,
or whatever.

Phil's response:
<However, writing all the glue can be boring and error prone, so there
<are a number of tools to make this easier. Take a look at
<http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/ and

PyRex intends to make it easy to write C extensions in extended Python.

<http://swig.sourceforge.net/ for ideas.

swig wraps existing extensions, which you seem to have.

Terry J. Reedy







More information about the Python-list mailing list