NumPy resize method

Viewed 7133

Can anyone explain this to me? (Python 3.3.2, numpy 1.7.1):

>>> a = np.array([[1,2],[3,4]])
>>> a    # just a peek
array([[1, 2],
       [3, 4]])
>>> a.resize(3,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot resize an array references or is referenced
by another array in this way.  Use the resize function
>>> a = np.array([[1,2],[3,4]])
>>> a.resize(3,2)
>>> a
array([[1, 2],
       [3, 4],
       [0, 0]])
>>> a = np.array([[1,2],[3,4]])
>>> print(a)   # look properly this time
[[1 2]
 [3 4]]
>>> a.resize(3,2)
>>> a
array([[1, 2],
       [3, 4],
       [0, 0]])

Why does taking a peek at the array create a reference to it? (or, at least, why does that reference persist after I'm done looking?) Also, is it just me or does that Exception need a bit of a rewrite?

1 Answers
Related