[C++-sig] Creating a python class from c++

James Healey healeyjames at yahoo.co.uk
Wed Nov 15 13:36:13 CET 2006


> James Healey wrote:
> > Can any body tell me how you create a python class
> > from within c++?
> > 
> > so in my .py file I have 
> > 
> > class MyClass:
> >    def f(self):
> >       return 'hello world'
> > 
> > 
> > And from my c++ code i want to be able to do....
> > 
> > x = new MyClass
> > x.f()
> 
> namespace bpl = boost::python;
> bpl::dict global;
> bpl::exec_file("your_file.py", global, global);     
> // load the definition
> bpl::object my_class = global["MyClass"];           
> // extract the new type
> bpl::object my_instance = my_class();               
> // instantiate it
> bpl::object retn = my_instance.attr("f")();         
> // call it
> std::string value = bpl::extract<std::string>(retn);
> // extract return value
> 
> HTH,
> 		Stefan

I dont seem to have the exec_file command in my
current boost build, I'm using 1.33.1 which was
downloaded via the boost website.



Send instant messages to your online friends http://uk.messenger.yahoo.com 



More information about the Cplusplus-sig mailing list