In [ 11 ]: t = ( 1 , 2 , [ 30 , 40 ]) In [ 12 ]: t [ 2 ] += [ 50 , 60 ] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - TypeError Traceback ( most recent call last ) ip
In [12]: t[2] += [50, 60]
------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-d877fb0e9d36> in <module>
----> 1 t[2] += [50, 60]
TypeError: 'tuple' object does not support item assignment
In [13]: t
Out[13]: (1, 2, [30, 40, 50, 60])
In [14]: t[2].append([70, 80])
In [15]: t
Out[15]: (1, 2, [30, 40, 50, 60, [70, 80]])
In [16]:
- 不要把可变对象放在元组里面
- 增量赋值不是一个原子操作,虽然抛出了异常,但还是完成了操作
- python字节码有助于了解代码背后的运行机制