-2

I'm trying to create a class that inherits numpy.ndarray, but I want to initialize it using a function that returns an ndarray. Specifically I want to generate an ndarray from a CSV file using the convenient numpy.genfromtxt() function. For example (I'm aware that this code does not do what I want it to):

class MyClass(numpy.ndarray):
    def __init__(self, other_parameter):
        self = numpy.genfromtxt(filename, delimiter=',')
        self.other_param = other_parameter

The point is that I want to extend ndarray a little bit by adding other features/attributes without losing the interface to ndarray. I know I can cobble together attributes in a class with one being the ndarray. Is extending it possible using a constructor (within my class definition __init__()) that isn't super().__init__()?

I've found answers to this type of thing for several other languages, but not Python. I've wanted to use similar init functions before, and have never found how to do this.

  • 1
    What you're looking for is calling `super()` - take a look at [this post](http://stackoverflow.com/questions/2399307/how-to-invoke-the-super-constructor) – Robert Seaman Apr 24 '17 at 23:27
  • @RobertSeaman: `super()` will not be helpful, because `numpy.genfromtxt` isn't the `numpy.ndarray` constructor method. `numpy.genfromtxt` returns a new array rather than initializing an uninitialized one. – user2357112 Apr 24 '17 at 23:29
  • 4
    Subclassing from `numpy.ndarray` is non-trivial. See the [docs](https://docs.scipy.org/doc/numpy/user/basics.subclassing.html#implications-for-subclassing). – juanpa.arrivillaga Apr 24 '17 at 23:31
  • The best you can do is copying the array returned by `genfromtxt`; you can't have `genfromtxt` initialize your `MyClass` object directly. – user2357112 Apr 24 '17 at 23:33
  • 1
    Regardless, if this is all you want inheritance seems like overkill. – juanpa.arrivillaga Apr 24 '17 at 23:36
  • @user2357112 hmm, just curious, but couldn't the array returned form `genfromtxt` act as the underyling `buffer` for the `__new__` array? – juanpa.arrivillaga Apr 24 '17 at 23:38
  • I'm confused as to what you want to accomplish. – RobertB Apr 24 '17 at 23:42
  • @juanpa.arrivillaga: True, you could take a view over it instead of copying. You can't force the `genfromtxt` return value into being a `MyClass` instance, but you can reuse its buffer. – user2357112 Apr 24 '17 at 23:42
  • @user2357112 sounds like a lot of dark magic just to wrap `genfromtext` in a constructor... – juanpa.arrivillaga Apr 24 '17 at 23:43
  • Why subclass at all? Why not just have the array be an attribute of MyClass and then initialize it however you want? What are you gaining from the inheritance? – RobertB Apr 25 '17 at 00:43
  • @juanpa: Thanks! That looks very helpful. Maybe it is overkill. So far I've handled such situations by calling the constructor and making that super-class instance an attribute of my class, but that is clunkier than I hoped for. I am trying to make a new class that acts just like `ndarray` with some extra features, which is the point of inheritance in my view. – Darryl Wade Apr 25 '17 at 13:18

1 Answers1

0

Credit for this answer belongs mostly to juanpa.arrivillaga for the comment above. The answer is this section of the link provided. The rest of the information from that whole doc page is helpful to understand it fully.

The short answer is that one uses the __new__() constructor (standard Python that I didn't know about yet) and "view casting", which is special ndarray functionality, within the custom class definition.

Community
  • 1
  • 1