-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[QUESTION] Proper way to handle std::span<T>-like types #2713
Comments
@psalvaggio What would you expect the corresponding Python type to be? Indeed, Do note that NumPy & pybind11 support custom struct-of-primitive-types dtypes: https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html#structured-types |
@YannickJadoul yes, I gave the NumPy structured types a try and it does work, although I do wind up losing the field names using that approach. I'd really like a NumPy array or something similar, but instead of getting |
@psalvaggio I don't understand. Normally, |
@YannickJadoul yes, So I have the following structs: template <typename T>
struct Vector3 {
T x, y, z;
};
struct ElementType {
double time;
Vector3<double> vec;
}; I have a method which returns a auto subM = m.def_submodule("submodule");
auto subsubM = subM.def_submodule("subsubmodule");
py::class_<Vector3<double>>(subM, "Vector3d", "Vector of 3 doubles")
.def_readwrite("x", &Vector3<double>::x)
.def_readwrite("y", &Vector3<double>::y)
.def_readwrite("z", &Vector3<double>::z);
PYBIND11_NUMPY_DTYPE(Vector3<double>, x, y, z);
py::class_<ElementType>(subsubM, "ElementType")
.def_readwrite("time", &ElementType::time)
.def_readwrite("vec", &ElementType::vec);
PYBIND11_NUMPY_DTYPE(ElementType, time, vec); then, when I'm binding my method: myClass.def(
"method",
[](py::object& obj, size_t index) {
auto& o = obj.cast<MyClass&>();
auto mySpan = o.method(index);
return pybind11::array_t<ElementType>(mySpan.size(), mySpan.data(), obj);
}); An example I get out of this is:
I was expecting to be able to get that first |
Isn't that just how NumPy's structured arrays work? That's nothing pybind11 can do anything about? What you could also do is return some kind of a list of
|
Yes, that appears to be an issue on NumPy's end rather than on pybind11's end. I'll look into |
OK, great. Let's close this then. |
I have a C++11 backported
std::span<T>
type in my code that I am trying to bind into Python and was unsure about the proper way to do this, as there is not built-in conversion for span to use as an example.I have had success using NumPy for span's of arithmetic types using the following:
however, when I try this with a struct type, I get the following error:
So, my question is, what is the proper way to handle binding such a span of objects? Is it using this NumPy approach and figuring out a way to register my type (which is already bound via
py::class_
) or is there another way to handle this?I also tried using
memoryview
, but I was unable to figure out the format string, as the docs say to just useformat_descriptor<T>::value
, but I got an error about this being undefined.Thanks,
Phil
The text was updated successfully, but these errors were encountered: