[Numpy-discussion] Expanding an array by assignment

Fernando Perez Fernando.Perez at colorado.edu
Thu Feb 17 12:01:15 EST 2005

Travis Oliphant wrote:

> I'm not sure what you meant by this.  Did you mean being able to expand 
> an array only by assignment?
> i.e.
> a = zeros(2)  a (2,1) array
> a[1,0] = 1    (resizes a behind the scenes to a 2x2 array and then sets 
> the 1,0 element)?

Mmmh.  I'm not sure I like the idea of an assignment triggering a silent 
resize/reshape event.  Explicit is better than implicit and all that...

I could see this kind of magical behavior easily causing silent, extremely 
hard to find bugs in a big program.

I may be missing something, but I'd be -1 on this.

The 'invalid indices in slices' is basically just sytnactic sugar for a 
try/except block, and it's well-documented behavior in the base language, 
across all its sequence types:

In [2]: ll=[]

In [3]: tt=()

In [4]: ss=''

In [5]: ll[0:1]
Out[5]: []

In [6]: tt[0:1]
Out[6]: ()

In [7]: ss[0:1]
Out[7]: ''

So in my view at least, this behavior of python isn't a good justification for 
a silent resize/reshape (which could, I'm sure, be also potentially explosive 
memory-wise) in numerix arrays.



More information about the NumPy-Discussion mailing list