STINNER Victor added the comment:
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])
----------
nosy: +haypo, rhettinger, serhiy.storchaka
versions: +Python 3.3, Python 3.4
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue19958>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com