This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Assignment success despite TypeError exception
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Dmitrii.Dimandt, ezio.melotti, rhettinger, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2013-12-12 08:37 by Dmitrii.Dimandt, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg205945 - (view) Author: Dmitrii Dimandt (Dmitrii.Dimandt) Date: 2013-12-12 08:36
Observed behaviour:

Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
>>> a = (1, [2,3])
>>> a[1] += [4,5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> a
(1, [2, 3, 4, 5])


Expected behaviour: tuple remains unchanged
msg205946 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-12-12 08:43
Oh, this behaviour is really weird. It can probably be explained by the fact that the INPLACE_ADD operator is used. See the bytecode for an explanation.

I don't know if it's possible to workaround this issue.

$ python3
Python 3.3.2 (default, Nov  8 2013, 13:38:57) 
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a=(1,[])
>>> a[1].append(2)
>>> a
(1, [2])
>>> a[1]+=[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> a
(1, [2, 3])
>>> def bug(a):
...  a[1] += [4]
... 
>>> import dis
>>> dis.dis(bug)
  2           0 LOAD_FAST                0 (a) 
              3 LOAD_CONST               1 (1) 
              6 DUP_TOP_TWO          
              7 BINARY_SUBSCR        
              8 LOAD_CONST               2 (4) 
             11 BUILD_LIST               1 
             14 INPLACE_ADD          
             15 ROT_THREE            
             16 STORE_SUBSCR         
             17 LOAD_CONST               0 (None) 
             20 RETURN_VALUE         
>>> a
(1, [2, 3])
>>> bug(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in bug
TypeError: 'tuple' object does not support item assignment
>>> a
(1, [2, 3, 4])
msg205947 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-12-12 08:54
http://docs.python.org/3/faq/programming.html#why-does-a-tuple-i-item-raise-an-exception-when-the-addition-works
History
Date User Action Args
2022-04-11 14:57:55adminsetgithub: 64157
2013-12-12 08:54:46ezio.melottisetstatus: open -> closed

type: behavior

nosy: + ezio.melotti
messages: + msg205947
resolution: not a bug
stage: resolved
2013-12-12 08:43:42vstinnersetnosy: + rhettinger, vstinner, serhiy.storchaka

messages: + msg205946
versions: + Python 3.3, Python 3.4
2013-12-12 08:37:00Dmitrii.Dimandtcreate