[Python-Dev] odd "tuple does not support assignment" confusion...

Alex A. Naanou alex.nanou at gmail.com
Sat Mar 3 00:06:33 CET 2012


Hi everyone,

Just stumbled on a fun little thing:

We create a simple structure...

  l = ([],)


Now modify the list, and...

  l[0] += [1]


...we fail:
## Traceback (most recent call last):
##   File "F:\work\ImageGrid\cur\ImageGrid\src\test\python-bug.py",
line 17, in <module>
##     l[0] += [1]
## TypeError: 'tuple' object does not support item assignment

Tested on 2.5, 2.7, 3.1, PyPy1.8 on win32 and 2.7 on x86-64 Debian
(just in case).


I was expecting this to succeed, is this picture wrong or am I missing
something?

...am I really the first one to try and modify a list within a tuple directly?!
It's even more odd that I did not try this myself since first started
with Python back in 99 :)
I could not google this "feature" out either...


BTW, It is quite trivial (and obvious) to trick the interpreter to get
the desired result...

  e = l[0]
  e += [1]


P.S. the attachment is runnable version of the above code...

-- 
Thanks!
Alex.


More information about the Python-Dev mailing list