Re: [C++-sig] Re: Static, again...
by David Abrahams other posts by this author
Nov 15 2002 12:28PM messages near this date
[C++-sig] Re: Static, again...
|
Re: [C++-sig] Static, again...
"Mike Rovner" <mike@[...].com> writes:
> "Hugo van der Merwe" <hugo@[...].za> wrote in message
> news:20021114053940.GB16542@[...]..
> > On Wed, Nov 13, 2002 at 08:19:53AM -0500, David Abrahams wrote:
> > > Hugo van der Merwe <hugo@[...].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@[...].com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution
_______________________________________________
C++-sig mailing list
C++-sig@[...].org
http://mail.python.org/mailman/listinfo/c++-sig
Thread:
Hugo van der Merwe
Scott A. Smith
David Abrahams
Scott A. Smith
Nicodemus
Scott A. Smith
Mike Rovner
David Abrahams
David Abrahams
Hugo van der Merwe
Hugo van der Merwe
David Abrahams
Hugo van der Merwe
David Abrahams
|