[Numpy-discussion] resize()

Filip Wasilewski filip at ftv.pl
Thu May 11 16:00:04 EDT 2006


Hi joris,

> I was surprised by the following effect of resize()

>>>> from numpy import *                    # 0.9.6
>>>> a = array([1,2,3,4])
>>>> a.resize(2,2)
>>>> a
> array([[1, 2],
>        [3, 4]])
>>>> a.resize(2,3)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ValueError: cannot resize an array that has been referenced or is referencing
> another array in this way.  Use the resize function

> Where exactly is the reference? I just started the interactive python shell,
> did nothing else...


You have also typed
>>> a

which in turn prints repr() of the array and causes some side effect in
the interactive mode (the `a` array is also referenced by _ special
variable after this). Try running this code as a script or use `print a`:

a.resize(2,2)
>>> print a
[[1 2]
 [3 4]]
>>> a.resize(2,3)
>>> print a
[[1 2 3]
 [4 0 0]]


> On the other hand, restarting python and executing

>>>> from numpy import *
>>>> a = array([1,2,3,4])
>>>> a.resize(2,3)
>>>> a
> array([[1, 2, 3],
>        [4, 0, 0]])

> does work...

Yes, no extra referencing before array resizing here.

> Why didn't it work for the first case?

This is just a small interactive mode feature and does not happen during
normal script execution.

cheers,
fw





More information about the NumPy-Discussion mailing list