[Numpy-discussion] array allocation using tuples gives views of same array

Hans Meine meine at informatik.uni-hamburg.de
Thu Nov 15 11:11:35 EST 2007


Am Donnerstag, 15. November 2007 16:29:12 schrieb Warren Focke:
> On Thu, 15 Nov 2007, George Nurser wrote:
> > It looks to me like
> > a,b = (zeros((2,)),)*2
> > is equivalent to
> > x= zeros((2,))
> > a,b=(x,)*2
>
> Correct.
>
> > If this is indeed a feature rather than a bug, is there an alternative
> > compact way to allocate many arrays?
>
> a, b = [zeros((2,)) for x in range(2)]

Let me add that this is a standard Python caveat, which also happens with 
lists -- many of us have once tried to initialize an array of empty lists 
with ([], ) * N, which results in N references to the same empty list.  
Warren pointed out the standard solution above.

-- 
Ciao, /  /
     /--/
    /  / ANS



More information about the NumPy-Discussion mailing list