0

I'm wondering if boost.python allows C++ functionality to be exposed to python after the module has loaded. For example it would be nice if something like this might work:

#include <boost/python.hpp>

int a;

void expose_var() {
    boost::python::scope().attr( "a" ) = a;
}

BOOST_PYTHON_MODULE( mod )
{
    boost::python::def( "expose_var", expose_var, "Expose an attribute." );
}

Then in python:

import mod
mod.expose_var()
mod.a = 2

With similar code I'm getting an error when I call the equivalent of expose_var():

AttributeError: 'NoneType' object has no attribute '__dict__'

I want to do this as I'm exposing a C++ library that is heavily templated and I don't wish to expose every possible combination of template parameters by default. I'd like to let the python client ask for specific combinations to be exposed at runtime.

Epimetheus
  • 1,119
  • 1
  • 10
  • 19
  • 2
    C++ templates are handled during the compilation, what you're asking for is runtime templates. – Tom van der Woerdt Feb 14 '13 at 14:56
  • Yes that's a good point. The code that exposes the library would have to be compiled with all combinations of template instantiations. However if you could expose after module load then the user could select what subset of functionality was exposed. Perhaps this is not as useful as I thought because all combinations must still be compiled up front. – Epimetheus Feb 14 '13 at 15:17

0 Answers0