[C++-sig] boost::python embedding return value

Stefan Seefeld seefeld at sympatico.ca
Thu Mar 4 20:08:20 CET 2010


On 03/04/2010 01:59 PM, Neal Becker wrote:
>
> OK.  I had assumed, since it returned an object, that I could call a
> function and get back the python result.  I didn't use eval, because eval is
> restricted to 1 expression, and so couldn't do something like:
> '''
> sys.path.append('./')
> import module
> module.F (3)'''
>
> which is what I was trying to do
>    

OK. If you want to do this once, this seems a good approach. However, 
for doing it repeatedly it might be better to first execute the 
"startup" code, such that "module" ends up in a dictionary ("global", in 
my case). Then you can run eval() on the expression you'd like to get 
the result of:

exec(code, global);
for(...)
{
     int result = extract<int>(eval("module.F(3)", global));
     ...
  }

Thus you only need to execute common startup code once.

         Stefan


-- 

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



More information about the Cplusplus-sig mailing list