[Numpy-discussion] NumPy-Discussion Digest, Vol 38, Issue 52

Jake VanderPlas jakevdp@gmail....
Thu Nov 12 13:40:46 CST 2009


> I'm rapidly losing interest here. Perhaps you could supply some code
> implementing this new array? Why not just a class using an array that
> doubles the array size when an index is out of bounds and copies over the
> old data. That is pretty much what realloc does. As to python lists, do you
> have any benchmarks showing how bad python lists are compared to arrays?
>
> Chuck

It sounds like all of this could be done very simply without going to
C, using a class based on numpy.ndarray.  The following works for 1D
arrays, behaves like a regular 1D numpy array, and could be easily
improved with a little care.  Is this what you had in mind?

import numpy

#easy scalable array class
class scarray:
    def __init__(self,*args,**kwargs):
        self.__data = numpy.ndarray(*args,**kwargs)

    def append(self,val):
        tmp = self.__data
        self.__data = numpy.ndarray(tmp.size+1)
        self.__data[:-1] = tmp
        self.__data[-1] = val
        del tmp

    def __getattr__(self,attr):
        return getattr(self.__data,attr)

x = scarray(5)
x[:] = numpy.arange(5)
print x
x.append(5)
print x


More information about the NumPy-Discussion mailing list