[C++-sig] Re: Static, again...

David Abrahams dave at boost-consulting.com
Fri Nov 15 13:28:29 CET 2002


"Mike Rovner" <mike at bindkey.com> writes:

> "Hugo van der Merwe" <hugo at adept.co.za> wrote in message
> news:20021114053940.GB16542 at vervet.localnet...
>> On Wed, Nov 13, 2002 at 08:19:53AM -0500, David Abrahams wrote:
>> > Hugo van der Merwe <hugo at adept.co.za> writes:
>> >
>> > Yes, but I'll leave it as an "exercise for the reader". Properties are
>> > a Python 2.2 thing, not a Boost.Python feature. Try it in pure
>> > Python. If you can get that to work, you can probably get it to work
>> > in Boost.Python. Hint: you'll need a proxy class.
>
> If I understand correctly
> Pythons
>   x.a[3]=1
> will go through
> __setitem__(__getattr__(x,"a"), 3, 1)
>
> If so, it shall be easy (untested):
>
> struct proxy {
>   GetItem();
>   SetItem():
> };
> struct X {
>   proxy a;
> }
>
> class_<proxy>("proxy")
>   .def("__getitem__", &proxy::GetItem)
>   .def("__setitem__", &proxy::SetItem)
> ;
>
> class_<X>("X")
>   .add_property("a", &X::a)
> ;
>
> Right?

No, object(&X::a) doesn't do anything useful (see the add_property
docs for why this is relevant).

    .def_readonly("a",&X::a)

would work, but I didn't think that was what you wanted. Did you
really want to modify your C++ X instance by adding a data member,
just to get the Python to work?

I had in mind:

proxy getter(X&) { return proxy(); }
...
 class_<X>("X")
   .add_property("a", getter)
 ;


-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list