[C++-sig] Boost Python - class_<T> As object

Stefan Seefeld seefeld at sympatico.ca
Tue Jun 7 01:52:20 CEST 2005


Dan Eloff wrote:

> This is nice, but I'm wondering about the inline class_<> declaration.
> Specifically I don't want to copy and paste the entire class_<> stuff
> everytime I want to create a python object of a exported c++ class and
> use it in c++. This violates the rule of duplication as a bad thing.
> How could I avoid this? Looking at it as I write, I think it may be
> possible to save the entire thing as a C++ variable and then use it
> elsewhere.
> 
> class_<Vec2> g_Vec2 = class_<Vec2>("Vec2", init<double, double>())
>         .def_readonly("length", &Point::length)
>         .def_readonly("angle", &Point::angle);
> 
> And then I use it like so ?:
> 
> object vec345 = g_Vec2(3.0,4.0);
> object vec_other = g_Vec2(7.0,7.0);
> 
> And use it inside the module declaration as well:
> 
> BOOST_PYTHON_MODULE(mymodule)
> (
> g_Vec2;
> ...
> )
> 
> I may be totally off here, but hopefully I've given a clear picture of
> what I'm trying to do. I just want to put everything in one place if I
> can.

I think you got it right. 'g_Vec2' represents a (python) type, and
'g_Vec2(...)' invokes __call__ on it, which is a constructor call in this
case, i.e. a factory that returns an instance of type 'g_Vec2'.

Regards,
		Stefan



More information about the Cplusplus-sig mailing list