Add NumPy Scalars#3544
Conversation
|
Logging my thoughts after looking for a bit. That's a lot of code (cost). It makes sense to distinguish between ints of different sizes etc. for large arrays, but how often does it matter for scalars in real life? Passing a one-element array could do the trick, too. Probably a minor nuisance that's worth spending time and code on only if it becomes wide-spread clutter. I will wait for other maintainers to chime in. |
|
@rwgk I understand your thoughts, thanks for your suggestions. |
|
It is not sure whether the PR will be merged, so if you want to use this feature, you can directly use this patch. |
|
I won't speak to the cost/benefit ratio. But I thought I could mention that I have found myself writing a small wrapper function like this: template<typename T>
py::object create_numpy_scalar(T val) {
// usage requires initialized NumPy C-API (call _imoprt_array() before use)
py::object dt = py::dtype::of<T>();
PyObject * scal = PyArray_Scalar(&val, (PyArray_Descr*)dt.ptr(), py::int_(sizeof(T)).ptr());
return py::reinterpret_steal<py::object>(scal);
}to wrap e.g. a |
@bjodah, thank you very very much. My original idea was to transfer this part of the cost directly from the user to us. Whether it is |
I have been bitten by this in the past as well and the current inconsistency for 0-dim data (scalars) makes generic ND data handling on API interfaces pretty wild/complicated. As long as this goes in the separate |
|
I'm personally fine with following numpy more closely (that is, adding features already supported by numpy, within reason). |
Thanks for pointing out @ax3l, I hadn't given that enough weight previously. I see the CI is currently very unhappy (42 failing). @sun1638650145, after I see you got it back to green, I'll run the Google global testing with this PR. Could you please tag me with an |
OK, I'll try to fix the bug as soon as I can. |
Description
Compared with using
py::bufferto provide processingpy::numpy_scalaris more concise and easier to use. I think PR #2060 is a very good solution, but it seems to be abandoned. So I made a simple modification and resubmitted a new PR.📚 Documentation preview 📚: https://pybind11--3544.org.readthedocs.build/