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

Olivier Delalleau shish at keba.be
Sat Mar 31 09:51:00 EDT 2012


It doesn't work because numpy.append(a, ...) doesn't modify the array a
in-place: it returns a copy.
Then in your append method, doing "self = numpy.append(...)" won't have any
effect: in Python such a syntax means the "self" local variable will now
point to the result of numpy.append, but it won't modify the object that
self previously pointed to.
I didn't try it, but it should work with

def append(self, other):
    numpy.ndarray.append(self, other)

which will call the append method of the parent class numpy.ndarray,
modifying self in-place.

-=- Olivier

Le 31 mars 2012 02:25, Prashant Saxena <animator333 at yahoo.com> a écrit :

> Hi,
>
> I am sub-classing numpy.ndarry for vector array representation. The append
> function is like this:
>
>     def append(self, other):
>        self = numpy.append(self, [other], axis=0)
>
> Example:
> vary = VectorArray([v1, v2])
> #vary = numpy.append(vary, [v1], axis=0)
> vary.append(v1)
>
> The commented syntax (numpy syntax) is working but "vary.append(v1)" is
> not working.
>
> Any help?
>
> Cheers
>
> Prashant
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120331/10137059/attachment.html>


More information about the NumPy-Discussion mailing list