<br><br><div class="gmail_quote">On Nov 15, 2007 9:11 AM, Hans Meine <<a href="mailto:meine@informatik.uni-hamburg.de">meine@informatik.uni-hamburg.de</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Am Donnerstag, 15. November 2007 16:29:12 schrieb Warren Focke:<br><div class="Ih2E3d">> On Thu, 15 Nov 2007, George Nurser wrote:<br>> > It looks to me like<br>> > a,b = (zeros((2,)),)*2<br>> > is equivalent to
<br>> > x= zeros((2,))<br>> > a,b=(x,)*2<br>><br>> Correct.<br>><br>> > If this is indeed a feature rather than a bug, is there an alternative<br>> > compact way to allocate many arrays?<br>
><br>> a, b = [zeros((2,)) for x in range(2)]<br><br></div>Let me add that this is a standard Python caveat, which also happens with<br>lists -- many of us have once tried to initialize an array of empty lists<br>with ([], ) * N, which results in N references to the same empty list.
<br>Warren pointed out the standard solution above.<font color="#888888"></font></blockquote><div><br>And I'll just add that another option in this case is to avoid tuples and lists altogether and just unpack a larger array:
<br><br>a, b = zeros([2,2])<br> <br>That's mostly inferior to the list comprehension solution in terms of clarity, but may appeal to the fans of the pithy. In some cases it also expresses intent better, but not often.
<br><br><br><OFF TOPIC EDITORIAL><br><br>Using lists for shapes is clearer both typographically: compare zeros((2,)) and zeros([2]), and conceptually: shapes are closer to lists (variable length, homogeneous) than tuples (fixed length, inhomogeneous).  Tuples are used for the shape attribute since they need to be immutable, but there's no reason to type them that way; it just makes things hard to read.
<br><br><OFF TOPIC EDITORIAL><br><br><br></div></div>-- <br>.  __<br>.   |-\<br>.<br>.  <a href="mailto:tim.hochberg@ieee.org">tim.hochberg@ieee.org</a>