[Numpy-discussion] ndarray sub-classing and append function

Chris Barker chris.barker at noaa.gov
Sun Apr 1 23:36:32 EDT 2012


On Sun, Apr 1, 2012 at 8:19 AM, Tom Aldcroft
<aldcroft at head.cfa.harvard.edu> wrote:
> You might try something like below (untested code, just meant as
> pointing in the right direction):
>
> self.resize(len(self) + len(v1), refcheck=False)
> self[len(self):] = v1
>
> Setting refcheck=False is potentially dangerous since it means other
> references to the object might get corrupted.  You should play with
> this option, but the alternative is that if there are *any* references
> to the object then the append will fail.

exactly -- numpy arrays are not designed to be re-sizable, and there
are good reasons for that.

I'd suggest that you either:

1) don't have an append method at all (though maybe provide a method
or function that makes a copy, like the numpy.append)

2) use a "has a" relationship, rather than subclassing -- i.e have
your class use a numpy array as container internally, even though it
isn't a numpy array.
  - you could still delegate most operations to the numpy array

-Chris



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list