a=([],); a[0]+=[1]

Andrea Griffini agriff at tin.it
Sat May 1 11:18:16 EDT 2004


When reading about python "warts" i stepped in this
example I find quite puzzling:

   a = ( [] , )
   a[0] += [1]

this gives an error, because a tuple cannot be
modified. But if you print the content of "a"
it will show it has indeed been modified.

How can this happen ? if "+=" for list was called
then no modification to the tuple was requested and
so no error should have been raised; if instead "+"
and "=" was executed then after a failing assignment
the original list is safe... (or at least I thought
so; if you type "a[0] = [2]" you get the error and
"a" is not modified).

Is there an explanation simpler than investigating
on python sources ?

Andrea



More information about the Python-list mailing list