Replies: 2 comments
-
It looks like I can dynamically add a numpy array as is done under the docs section Object-oriented code > Dynamic attributes. This could save my project. [EDIT: if I could find a way how class methods could access the data after adding the attribute :-( ] Also I will give it a try to add the numpy array in the constructor. |
Beta Was this translation helpful? Give feedback.
-
It looks like more people struggle(d) with using numpy arrays in structs/classes. See the closed issue: Read/write dynamic attributes from C++ #1751. I came accross this because I also tried to add a numpy array attribute dynamically. The next problem is then: how to access the array data with methods of the same class. I am too far from being an expert to conclude that this is not possible. The route that I have in mind now is to bring numpy data to the C++ side with a function call. Then construct and use a C++ class that is fully on the C++ side. |
Beta Was this translation helpful? Give feedback.
-
[Edit]: after opening this discussion item I tranferred it to create an issue for it (#5036). As I come accross new insights in trying to work around this, I will add those here in Discussion #5035).
I want to create a C++ class that can use a Numpy array. So I started to read the docs on using object-oriented code and Numpy arrays. I tested the examples in these sections and they behave well.
My own code for a class MC_box_1 is based on the struct Pet example (pybind11 documentation > Object-oriented code) and the modify_array example (pybind11 documentation > Python C++ interface >> Numpy). Both examples behave OK but when I combine them in a class MC_box_1 I get an error that is similar to the one described as in issue #2274 and have no clue how to solve this. I hope that someone can be of help on this.
My next step would be to insert the array already in the constructor.
My code:
Although I use the same array and calling code as in the modify_array example I get an error on using the setArray method:
Beta Was this translation helpful? Give feedback.
All reactions