[Numpy-discussion] array allocation using tuples gives views of same array
Matthieu Brucher
matthieu.brucher at gmail.com
Thu Nov 15 10:23:27 EST 2007
It's not a question of tuple, you made a tuple, but in each element, you put
the same array, so this behaviour is to be expected.
Matthieu
2007/11/15, George Nurser <gnurser at googlemail.com>:
>
> 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.
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
--
French PhD student
Website : http://miles.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn : http://www.linkedin.com/in/matthieubrucher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20071115/b808adde/attachment.html>
More information about the NumPy-Discussion
mailing list