How do I wrap a static member function?
Hi, I have the following code: ------------------------------------------------------------------------------- #include <boost/python.hpp> #include <iostream> class Foo { public: Foo() {} ; static void doit(const char * message) { std::cout << "doing it statically: " << message << "\n"; }; void nonstatic_doit(const char *message) { std::cout << "doing it nonstatically: " << message << "\n"; }; }; using namespace boost::python; BOOST_PYTHON_MODULE(foo) { class_<Foo>("Foo",init<>()) .def("doit", &Foo::doit) .def("nonstatic_doit", &Foo::nonstatic_doit) ; } ------------------------------------------------------------------------------- When I try out my foo module, I see this: In [1]: from foo import Foo In [2]: f = Foo() In [3]: f.nonstatic_doit("hello") doing it nonstatically: hello In [4]: f.doit("hello") --------------------------------------------------------------------------- ArgumentError Traceback (most recent call last) /home/amundson/work/synergia2-devel_1_0/build/synergia2/components/foundation/<ipython console> in <module>() ArgumentError: Python argument types in Foo.doit(Foo, str) did not match C++ signature: doit(char const*) ------------------------------------------------------------------------------- How can I wrap doit? Thanks, Jim Amundson
Use class_.staticmethod(...) as shown below: On Friday 30 October 2009 17:47:45 James Amundson wrote:
BOOST_PYTHON_MODULE(foo) { class_<Foo>("Foo",init<>()) .def("doit", &Foo::doit)
.staticmethod( "doit" )
.def("nonstatic_doit", &Foo::nonstatic_doit) ; }
Regards, Ravi
participants (2)
-
James Amundson -
Ravi