python assignment
Steven Taschuk
staschuk at telusplanet.net
Thu Jul 24 23:59:12 EDT 2003
Quoth dan:
[...]
> 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?
Afaik it's not documented, but I think you may safely assume that
arithmetic on lists will always produce a new list, even in such
cases as
a_list + []
1*a_list
Having such expressions possibly return the existing object is a
harmless optimization for immutable types, but would be madness
for mutable objects such as lists. Guido is not mad. (A little
eccentric sometimes, occasionally silly, and noticeably Dutch, but
not mad.)
> I suppose a clearer fix would be v[x] = copy.copy(temp), eh?
Yes, that, or
v[x] = list(temp)
or
v[x] = temp[:]
according to taste. The latter is most common, I think.
--
Steven Taschuk staschuk at telusplanet.net
"[T]rue greatness is when your name is like ampere, watt, and fourier
-- when it's spelled with a lower case letter." -- R.W. Hamming
More information about the Python-list
mailing list