[C++-sig] Need strategic advice designing Boost/Python program

Stefan Seefeld seefeld at sympatico.ca
Thu Apr 26 23:54:46 CEST 2007


Craig Finch wrote:
> As a fairly new boost.python user, I would like some expert advice on
> which strategy to use in using Boost and Python, so that I don't go
> down a dead end.  A clean interface is more important to me than
> efficiency.

(Depending on what 'clean' means to you, it may not actually be in conflict
with efficient at all.)

> I am writing a numerical solver.  I want most of the code to be in
> Python for ease of development.  However, I want to implement some
> objects in C++ for speed, because they use iterative solution methods. 
> I envision my Python code as creating a mesh on a domain, calling a C++
> solver, and performing post-processing such as plotting.  The cleanest
> way to do this seems to be to create a complex "mesh" object in Python,
> and then pass all the mesh data to C++ at once.  However, I want to
> implement the methods for creating and modifying the mesh in Python,
> because flexibility is more important than speed.  After reading a lot,
> there seem to be three main approaches to doing this.  I need help
> choosing between:
> 
> 1. Create the data portions of the mesh class in C++ and use Boost to
> expose it to Python as a module.  Then use Python to add methods, as
> shown in the Tutorial, and pass the whole object back to C++.  Finally,
> use <extract> to get C++ values for computation.  One downside to this
> approach is that apparently Python can't be used to implement a
> constructor method for a wrapped C++ class.

I'm not sure what you mean by 'implement a constructor'. Can you elaborate ?
As a minor point, if you go this route, there is no need to use extract<>
to access values, as within C++, you'll always deal with the (native) C++
interface directly. extract<> is useful to pull out embedded C++ objects
from python (wrapper) objects, or to require an explicit conversion / cast.

> 2. Similar to #1, but instead create Python classes that inherit from
> the C++ base classes, and then add Python methods.  Not sure how to get
> the resulting objects back into C++.

If you add new methods, or, more generally, attributes, you'll have to
access them through the boost.python 'attr()' method that is part of the
object protocol. However, if these attributes are only added for convenience
(i.e. used within python, but not necessary for the C++ solvers), there
isn't any need to access them from within C++, right ?

> 3. Create the data and methods in Python, and then define custom lvalue
> converters to get the data into C++.  This seems messy because I have
> to use the classic Python/C API in the converter functions, which seems
> to kind of defeat the purpose of using Boost.

What would worry me more is that each time you cross the language boundary
you have to copy the data, which hurts performance. I understand that
efficiency isn't of great concern to you (right now), but there is no reason
to require that extra copy. I believe solutions 1 or 2 are in fact superior.

HTH,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list