[C++-sig] wrapping global variables

Dominique Devriese dominique.devriese at student.kuleuven.ac.be
Mon Oct 21 01:07:28 CEST 2002


On Sun, Oct 20, 2002 at 06:45:44PM -0400, David Abrahams wrote:
> Dominique Devriese <dominique.devriese at student.kuleuven.ac.be> writes:
> 
> > Hi,
> > ( please cc me as i'm not subscribed to the list)
> > I'm playing around a bit with embedding python in a C++ program.  I'm
> > using Boost.python for exposing the program's API to python, and the
> > normal python interface ( Py_Initialize, PyRun_SimpleString and
> > Py_Finalize ) for embedding.
> > 
> > So far this has proven surprisingly easy.  Especially Boost.Python has
> > amazed me with its ease of use, so congratulations to everyone here
> > who helped write it, as it most definitely rocks :)
> > 
> > But I have a question:  I would like to expose some global variables
> > to python.  E.g. if I have a variable curbuf, I would like to be able
> > to write ( in the embedded python interpreter, that is ) something
> > like "len = curbuf.length();".  curbuf is of class Buffer, and this
> > class has been exported very easily, but I don't seem to be able to
> > find how to expose a global variable.  
> 
> Take a look at the documentation for scope.hpp. What you're asking
> might be as easy as:
> 
>      scope().attr("len") = curbuf.length();
> 
> ...if I understand what you're asking correctly.

Thanks !
Actually, what i needed wasn't exactly that, but well, this:

"
static Buffer curbuf;

BOOST_PYTHON_MODULE_INIT(ee)
{
  python::class_<Buffer>( "Buffer" )
    .def( "length", &Buffer::length )
    ;
  python::scope().attr( "curbuf" ) = curbuf;
}
"

But it works great, sorry i didn't think of that myself...
cheers
domi

-- 
Dominique Devriese
http://users.pandora.be/frit/domi.gpg




More information about the Cplusplus-sig mailing list