boost::python - how to expose c++ instancess?

hi, i'd like to do something like this: -------------- CUT HERE -------------- #include <boost/python.hpp> using namespace boost::python; struct A { void f() { /* do something */ } }; A a; BOOST_PYTHON_MODULE(hello) { class_<A>("A") .def("f", &A::f) ; def_readwrite("a", a); } -------------- CUT HERE -------------- i don't see how to do it with boost. any hint for a poor newbie? thanks dom -----[ Domenico Andreoli, aka cavok --[ http://filibusta.crema.unimi.it/~cavok/gpgkey.asc ---[ 3A0F 2F80 F79C 678A 8936 4FEE 0677 9033 A20E BC50

cavok@filibusta.crema.unimi.it writes:
hi,
i'd like to do something like this:
-------------- CUT HERE -------------- #include <boost/python.hpp> using namespace boost::python;
struct A { void f() { /* do something */ } };
A a;
BOOST_PYTHON_MODULE(hello) { class_<A>("A") .def("f", &A::f) ;
def_readwrite("a", a); } -------------- CUT HERE --------------
i don't see how to do it with boost.
any hint for a poor newbie?
You can't make a writable module-scope variable because modules don't support anything like properties or __setattr__ methods. Consider how you would do a similar thing in Python, keeping in mind that when you assign into a you need to have a function invoked which writes into the C++ variable. If you look around (see the Python cookbook on http://www.aspn.com, ferinstance) I think you can find a trick by Alex Martelli which lets you spoof a module with a class. HTH. P.S. Boost.Python questions to the C++-sig, please: http://www.boost.org/mailing_lists.htm#cplussig -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
cavokļ¼ filibusta.crema.unimi.it
-
David Abrahams