How to return a python list of number without modify the cpp class definitions? #3638
foreverlms
started this conversation in
General
Replies: 1 comment 1 reply
-
If you do not want to modify C++ you always can create a wrapper around the original class: class FooWrapper
{
public:
FooWrapper(void *data, py::dtype type, std::vector<size_t> shape) : _type(type), _shape(shape)
{
Foo tmp_foo;
_foo = tmp_foo;
_foo.data = data;
}
Foo _foo;
py::dtype _type;
std::vector<size_t> _shape;
}; And create class bindings similar to this: py::class_<FooWrapper, std::shared_ptr<FooWrapper>> cls(module, "Foo");
cls.def(py::init([](py::array &data)
{
std::vector<size_t> shape(data.shape(), data.shape() + data.ndim());
return FooWrapper(const_cast<void *>(data.data()), data.dtype(), shape);
}),
py::arg("data"));
cls.def_property_readonly(
"data", [](FooWrapper &self)
{ return py::array(self._type,
self._shape,
self._foo.data,
py::cast(self)); }); If in your case you have no other means of extracting info like size/number of elements/byte size of element about void* from your class... I think it would be non-trivial or nearly impossible to get information about array you want. You need to have at least number of elements you are holding under void*. If there is any way to obtain them, you should return "raw data" -- that means flat list with shape of number of elements and type of uint8 for example. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Suppose I have a class:
So the data pointer will be filled with numbers (float/int...),I export this class(m is the module I defined):
How can I define a member method in python
Foo
to return the data in pointerdata
without modify c++Foo
? thanks.Just like:
Beta Was this translation helpful? Give feedback.
All reactions