[Numpy-discussion] List Array References?

Stefan van der Walt stefan@sun.ac...
Fri Feb 15 10:51:45 CST 2008


On Fri, Feb 15, 2008 at 08:28:08AM -0500, Alexander Michael wrote:
> On Fri, Feb 15, 2008 at 7:12 AM, Stefan van der Walt <stefan@sun.ac.za> wrote:
> >  As far as I know, the reference count to the array is increased when
> >  you create a view, but the views themselves are not tracked anywhere.
> >  You can therefore say whether there are references around, but you
> >  cannot identify the Python objects.
> 
> I would like to occasionally dynamically grow an array (i.e. add
> length to existing dimensions) as I do not know th ultimately required
> length beforehand, but I have a good guess so I won't be need to
> reallocate that often if at all. The only way I know how to do this is
> numpy is to create a new larger array with the new dimensions and copy
> the existing data from the smaller array into it. Perhaps there is a
> better way that doesn't invalidate views on the array? I want to make
> sure that there are no outstanding references to the old array (i.e.
> views on it, etc.) so that I can raise a helpful exception while
> developing.

Numpy does complain if you attempt to resize an array with views on
it:

  In [8]: x = np.array([1,2,3])

  In [14]: y = x[::-1]

  In [18]: x.resize((4,))
  ---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)

  /tmp/<ipython console> in <module>()

  ValueError: cannot resize an array that has been referenced or is
  referencing another array in this way.  Use the resize function

You can catch that exception and work from there.  I hope that is what
you had in mind?

Regards
Stéfan


More information about the Numpy-discussion mailing list