[Python 3]
I like ndarray but I find it annoying to use.
Here’s one problem I face. I want to write class Array that will inherit much of the functionality of ndarray, but has only one way to be instantiated: as a zero-filled array of a certain size. I was hoping to write:
class Array(numpy.ndarray):
def __init__(size):
# What do here?
I’d like to call super().__init__ with some parameters to create a zero-filled array, but it won’t work since ndarray uses a global function numpy.zeros (rather than a constructor) to create a zero-filled array.
Questions:
-
Why does ndarray use global (module) functions instead of constructors in many cases? It is a big annoyance if I’m trying to reuse them in an object-oriented setting.
-
What’s the best way to define
class Arraythat I need? Should I just manually populatendarraywith zeroes, or is there any way to reuse thezerosfunction?
If you don’t like
ndarrayinterface then don’t inherit it. You can define your own interface and delegate the rest to ndarray and numpy.