python assignment

Terry Reedy tjreedy at udel.edu
Fri Jul 25 20:02:23 CEST 2003


"dan" <danbmil99 at yahoo.com> wrote in message
news:fbf8d8f2.0307241825.bd3b315 at posting.google.com...
> >>> for x in range(3):
> ...  for y in range(3):
> ...   temp[y] = x+y
> ...  v[x] = temp + []
> ...
> >>> v
> [[0, 1, 2], [1, 2, 3], [2, 3, 4]] #expected behavior
>
> but again the +[] looks funky in the morning light.  Can I always
> assume that an operation of this sort will return a new object, even
> if it has no effect on one of the operands?
>
> I suppose a clearer fix would be v[x] = copy.copy(temp), eh?

or add temp = [None]*3[] between 'for x' and 'for y' so you start with
an explicitly new temp for each x loop.

Terry J. Reedy






More information about the Python-list mailing list