[Numpy-discussion] can't resize ndarray subclass
Zachary Pincus
zpincus at stanford.edu
Tue Feb 28 04:36:01 CST 2006
Thus far, it seems like the way to get instances of ndarray
subclasses is to use the 'view(subclass)' method on a proper ndarray,
either in the subclass __new__, or after constructing the array.
However, subclass instances so created *do not own their own data*,
as far as I can tell. They are just "views" on an other array's data.
This means that these objects can't be resized, or have other
operations performed on them which requires 'owning' the data buffer.
E.g.:
class f(numpy.ndarray):
def __new__(cls, *p, **kw):
return numpy.array(*p, **kw).view(cls)
f([1,2,3]).resize((10,))
ValueError: cannot resize this array: it does not own its data
numpy.array([1,2,3]).view(f).resize((10,))
ValueError: cannot resize this array: it does not own its data
numpy.array([1,2,3]).resize((10,))
(no problem)
Is there another way to create ndarray subclasses which do own their
own data?
Note that numpy.resize(f([1,2,3]), (10,)) works fine. But this isn't
the same as having an object that owns its data. Specifically,
there's just no way to have an ndarray subclass resize itself as a
result of calling a method if that object doesn't own its data.
(Imagine a variable-resolution polygon type that could interpolate or
decimate vertices as needed: such a class would need to resize itself.)
Zach
More information about the Numpy-discussion
mailing list