[Numpy-discussion] array allocation using tuples gives views of same array
George Nurser
gnurser@googlemail....
Thu Nov 15 09:17:06 CST 2007
I tried the (as I thought) nice compact form
In [60]: a,b = (zeros((2,)),)*2
But...
In [61]: b[0] = 2
In [62]: a
Out[62]: array([ 2., 0.])
a and b are the _same_ array....
But
In [68]: a,b = (zeros((2,)),zeros((2,)))
In [69]: b[0] = 2
In [70]: a
Out[70]: array([ 0., 0.])
is OK. a & b are independent in this case.
I'm puzzled by this behaviour, I suspect because of my ignorance of
how tuples work.
It looks to me like
a,b = (zeros((2,)),)*2
is equivalent to
x= zeros((2,))
a,b=(x,)*2
If this is indeed a feature rather than a bug, is there an alternative
compact way to allocate many arrays?
Regards, George.
More information about the Numpy-discussion
mailing list