[C++-sig] Re: registering simple conversions from/to python
Brett Calcott
brett.calcott at paradise.net.nz
Wed May 28 14:35:57 CEST 2003
"David Abrahams" <dave at boost-consulting.com> wrote:
> > ...<snip>
> >
> > I think Ralf has supplied enough information to get me started.
>
> Ralf's info is good, but it appears to have nothing to do with this
> problem.
>
> All you need is to use the add_property method with a function which
> takes a tuple:
>
>
> void set_xy(C& c, boost::python::tuple xy)
> {
> c.x = xy[0];
> c.y = xy[1];
>
-- it needs this:
c.x = extract<double>(xy[0]);
c.y = extract<double>(xy[1]);
> // should check that xy is only 2 elements long here
> }
>
> boost::python::tuple get_xy(C& c)
> {
> return boost::python::make_tuple(c.x, c.y);
> }
>
> class_<C>("C")
> .add_property("xy", get_xy, set_xy)
> ;
>
Wow! I was off on some wild goose chase making things complicated,
thinking I had to write custom converters..
This has got to count as one very funky technique. I expose my class,
and then add a bunch of pythonic functionality to it by declaring
functions of this kind.
Cheers, I'm off to bed.
Brett
More information about the Cplusplus-sig
mailing list