logo       

Re: registering simple conversions from/to python: msg#00304

python.c++

Subject: Re: registering simple conversions from/to python



"David Abrahams" <dave@xxxxxxxxxxxxxxxxxxxx> 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


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise