[C++-sig] Passing an instance...

Stefan Seefeld seefeld at sympatico.ca
Fri Nov 3 20:11:27 CET 2006


Beau Sapach wrote:
> Hello everyone,
> 
>  
> 
> I've got a C++ object that I've created a Python type for.  When I insert
> the object into the main module using PyModule_AddObject then I can
> instantiate it in Python and everything works just fine.  I assumed (mostly
> from reading other's code) that I could use something like:
> 
>  
> 
> PyDict_SetItemString(MainDict,"x",pMyObject)

Up to this point there is no need to use the C API directly. Assuming you
have exported your C++ type 'Type' to python, you can write (inside C++):

namespace bpl = boost::python;
bpl::object instance(Type(...));

bpl::dict global;
global["instance"] = instance;
bpl::exec_file(python_script, global, global);


and the executed script can access your Type instance directly through
the 'instance' variable.

Caution: the 'exec_file' function has been added after 1.33.1, so
you need to use a CVS snapshot.

Regards,
		Stefan

-- 

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



More information about the Cplusplus-sig mailing list