[Numpy-discussion] Resize Method for Numpy Array

Robert Kern robert.kern@gmail....
Thu Sep 24 14:27:03 CDT 2009


On Thu, Sep 24, 2009 at 09:58, Alice Invernizzi <invernizzi@cilea.it> wrote:
>
> Dear all,
>
> I have an Hamletic doubt concerning the numpy array data type.
> A general learned rule concerning the array usage in other high-level
> programming languages is that array data-type are homogeneous datasets
> of  fixed dimension.

While this description is basically true of numpy arrays, I would
caution you that every language has a different lexicon, and the same
word can mean very different things in each. For example, Python lists
are *not* linked lists; they are like C++'s std::vectors with a
preallocation strategy to make appending cheap on average.

> Therefore, is not clear to me why in numpy the size of an array can be
> changed  (either with the 'returning-value' resize() function either with
> the 'in-place' array method resize()).
> More in detail, if the existence of the first function
> ('returning-value') might make sense in array computing operation, the
> existence of the 'in-place' method really make no sense for me.
>
> Would you please be so kind to give some explanation for the existence of
> resize operator for numpy array? If array size can be change, what are the
> real advantages of using numpy array instead of list object?

The .resize() method is available for very special purposes and should
not be used in general. It will only allow itself to be used if there
were no views created from it; otherwise, it will raise an exception.
The reason is that it must necessarily reallocate memory for the new
size and copy the data over. Any views would be pointing to
deallocated data. .resize() can be useful when you are constructing
arrays from a stream of data of unknown length and you haven't let any
other code see the array, yet. It is not really a defining feature of
numpy arrays like the other features that Chris Barker has listed for
you.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list