Hi, Consider the following Singleton class, which seems to have problems being wrapped by Boost.Python. ******************************************************** class lustate { public: static lustate& instance(const vector<double>& lower, const vector<double>& upper) { static lustate lu(lower, upper); return lu; }; vector<double> l; vector<double> u; private: lustate(const vector<double>& _l, const vector<double>& _u): l(_l), u(_u) {}; }; **************************************************************** I tried BOOST_PYTHON_MODULE(mg) { boost::python::class_<lustate>("lustate", boost::python::no_init) // boost::python::class_<lustate>("lustate") .staticmethod("instance") ; } This compiles without error, but on importing I get ******************************************************************************* In [1]:import mg --------------------------------------------------------------------------- exceptions.KeyError Traceback (most recent call last) /home/faheem/co/paper/mg/c++/<console> KeyError: 'instance' ******************************************************************************** I think that static member functions still require the corresponding class to be instantiated in Python, since it is invoked like a regular bound method, though the values obtained are of course the same for every instantiation. This understanding is based on Googling + the example files staticmethod.cpp & staticmethod.py. In other words, it doesn't have the equivalent of lustate::instance at the Python level. Since the constructor is private, it cannot instantiate the class. Hence, it is stuck. Is this an accurate description of the situation? I'm not sure what the error means, though. Faheem.
Faheem Mitha <faheem@email.unc.edu> writes:
******************************************************** class lustate { public: static lustate& instance(const vector<double>& lower, const vector<double>& upper) { static lustate lu(lower, upper); return lu; }; vector<double> l; vector<double> u; private: lustate(const vector<double>& _l, const vector<double>& _u): l(_l), u(_u) {}; }; ****************************************************************
I tried
BOOST_PYTHON_MODULE(mg) { boost::python::class_<lustate>("lustate", boost::python::no_init) // boost::python::class_<lustate>("lustate")
You're missing .def("instance", &lustate::instance) right here.
.staticmethod("instance") ; }
HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams -
Faheem Mitha