[C++-sig] [Boost.Python] Adding a constructor to python object
Александров Петр
petr_aleksandrov at mail.ru
Tue Mar 5 12:13:12 CET 2013
I want to create a Python interface for
"boost::numeric::ublas::vector<TReal>" where TReal is a C++ class for
numbers. How can I add my constructor (to create a vector from a python
list) to the python interface for "ublas::vector"? The following approach:
class_<CVector>("vector")
.def(init<size_t>())
.def(init<list>())
.def("__getitem__", &vector_get_item)
.def("__setitem__", &vector_set_item)
.def("resize", &CVector::resize)
.def("size", &CVector::size);
where
class CVector: public ublas::vector<TReal> {
public:
...
CVector(const list &l) { ... }
};
doesn't works. The python program exits with the following message
Boost.Python.ArgumentError: Python argument types in
some_function(some_class, vector) did not match C++ signature:
some_function(..., boost::numeric::ublas::vector<...>)
I think that boost doesn't understand that "CVector" is practically
"ublas::vector<TReal>". Inheriting "CVector" from
"wrapper<ublas::vector<TReal> >" solves this problem. But I need to add
"boost::noncopyable" to "class_<CVector>" while I want to use a function
that returns "ublas::vector<TReal>" in python. Is it possible not to add
"boost::noncopyable" in such case? Numpy bindings are not appropriate
because I use "ublas::vector" with custom type for real numbers.
More information about the Cplusplus-sig
mailing list