Emulating Python class variables in C++
Hi there, I am porting a Python library to C++ - and re-exposing back to Python with boost (suffice to say the Python API is fixed). I am having problems emulating class variables from boost as C++ static data members using inheritance. Here's an example: using namespace boost::python; using namespace std; struct A { static string value; }; struct B : public A { static string value; }; string A::value("A"); string B::value("B"); BOOST_PYTHON_MODULE(test) { class_<A>("A") .def_readonly("value", &A::value); class_<B>("B") .def_readonly("value", &B::value); } If I don't specify inheritance using bases<>, then this works. But I need inheritance, so I write: BOOST_PYTHON_MODULE(test) { class_<A>("A") .def_readonly("value", &A::value); class_("B"); } But now in Python, I only have access to A: >>> from test import A, B >>> A.value 'A' >>> B.value 'A' If try and overload B::value, I get an error: BOOST_PYTHON_MODULE(test) { class_<A>("A") .def_readonly("value", &A::value); class_("B") .def_readonly("value", &B::value); } >>> from test import A, B Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: can't set attribute I've also tried switching .def_readwrite, but now the error is: >>> from test import A, B Traceback (most recent call last): File "<stdin>", line 1, in <module> Boost.Python.ArgumentError: Python argument types in None.None(Boost.Python.StaticProperty) did not match C++ signature: None(std::string) So, to summarise, any idea how to provide polymorphic Python class variables in boost::python? Thanks in advance for your help.
On 2/08/2015 20:05, Will Hall wrote:
struct A { static string value; };
struct B : public A { static string value; };
string A::value("A"); string B::value("B");
While I can't answer your question directly, I can tell you that this is bad coding practice in C++. Usually it stems from a misunderstanding, while other times it's just a bad design.
So, to summarise, any idea how to provide polymorphic Python class variables in boost::python?
Static/class fields cannot be polymorphic, by definition.
On August 2, 2015 8:41:08 PM EDT, Gavin Lambert
On 2/08/2015 20:05, Will Hall wrote:
struct A { static string value; };
struct B : public A { static string value; };
string A::value("A"); string B::value("B");
While I can't answer your question directly, I can tell you that this is bad coding practice in C++. Usually it stems from a misunderstanding, while other times it's just a bad design.
Have you considered type traits. Don't know how that works with python though. template<class T> struct policy { static const bool value = false; }; struct policy<A> { static const bool value = true; }; There's an interesting tutorial here: http://www.cantrip.org/traits.html
So, to summarise, any idea how to provide polymorphic Python class variables in boost::python?
Static/class fields cannot be polymorphic, by definition.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Sent from my Android device with K-9 Mail. Please excuse my brevity.
participants (3)
-
Gavin Lambert
-
Michael
-
Will Hall