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
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@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@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Sat, Mar 31, 2012 at 2:25 AM, Prashant Saxena <animator333@yahoo.com> wrote:
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?
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. - Tom
Cheers
Prashant
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Sun, Apr 1, 2012 at 8:19 AM, Tom Aldcroft <aldcroft@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@noaa.gov
participants (4)
-
Chris Barker -
Olivier Delalleau -
Prashant Saxena -
Tom Aldcroft