Custom data types in numpy arrays

后端 未结 3 1326
小蘑菇
小蘑菇 2020-12-02 23:09

I\'m creating a numpy array which is to be filled with objects of a particular class I\'ve made. I\'d like to initialize the array such that it will only ever contain object

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 00:06

    As far as I know, enforcing a single type for elements in a numpy.ndarray has to be done manually (unless the array contains Numpy scalars): there is no built-in checking mechanism (your array has dtype=object). If you really want to enforce a single type, you have to subclass ndarray and implement the checks in the appropriate methods (__setitem__, etc.).

    If you want to implement operations on a set of functions (Kernel objects), you might be able to do so by defining the proper operations directly in your Kernel class. This is what I did for my uncertainties.py module, which handles numpy.ndarrays of numbers with uncertainties.

提交回复
热议问题