|
Boost Users : |
Subject: [Boost-users] Emulating Python class variables in C++
From: Will Hall (boost_at_[hidden])
Date: 2015-08-02 04:05:43
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, bases<A> >("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, bases<A> >("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.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net