[C++-sig] Creating a python class from c++
Stefan Seefeld
seefeld at sympatico.ca
Sun Nov 19 23:43:48 CET 2006
James Healey wrote:
> If I have this:
>
> class Test
> {
> public:
> int iNumber;
>
> int getNumber(void) { return iNumber; }
> };
>
> BOOST_PYTHON_MODULE( extended_test )
> {
> boost::python::class_<Test>("Test")
> .def( "getNumber", &Test::getNumber )
> ;
> }
>
> boost::python::object my_class = global["MyTest"];
>
> How would I go about creating an instance of class
> Test and passing it to my python class instance?
>
> boost::python::object my_instance = my_class(test
> boost::python::object retn =
> my_instance.attr("TestFunc")();
I'm not sure what you are trying to accomplish. Are you asking how
to detach the lifetime of the C++ Test instance from the lifetime of
the Python Test instance ?
In that case it seems most suitable to export a C++ function
returning your (C++) Test instance, letting boost.python wrap the existing
instance, instead of instantiating the wrapper with a copy of it.
You may want to read the docs on call- / return-value policies, too.
HTH,
Stefan
--
...ich hab' noch einen Koffer in Berlin...
More information about the Cplusplus-sig
mailing list