-
This question might be too simple, but I read the docs several times and can't find a clear answer. Assume I want to return a ndarray, is it valid to do something like:
Will I get ownership problems or will nanobind copy the data and everything is fine? I understand that for larger objects as vectors, one would want to avoid copies, but for objects like arrays or fixed sized vectors, the code would stay much cleaner. Besides I wonder, if one could avoid the ownership code (e.g. capsules) for large structures, by letting the ndarray directly allocate it's own memory and then just e.g. use std::span and fill it, something like that:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Regarding the first problem: yes, you will get ownership problems. The What you request below is -- sort of -- possible. The problem is that
|
Beta Was this translation helpful? Give feedback.
Regarding the first problem: yes, you will get ownership problems. The
std::array
lives on the stack and will expire. You need some kind of container with a Python owner, as is explained in the documentation.What you request below is -- sort of -- possible. The problem is that
nb::ndarray
is just metadata describing where an array is to be found. You need to convert it into a Python object using a framework of choice (eg. NumPy) for an actual copy to be made. Something like the following (untested)