【发布时间】:2017-02-02 01:36:10
【问题描述】:
我正在阅读这篇有趣的帖子https://asmeurer.github.io/blog/posts/tuples/
作者在脚注处展示了这个例子
>>> t=1,2,[3,4]
>>> t
(1, 2, [3, 4])
>>> t[2]+=[5,6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
虽然 Python 引发了异常,但它确实改变了元组
>>> t
(1, 2, [3, 4, 5, 6])
不确定这里发生了什么,这是一个错误吗?
2.7.10 和 3.5.1 中的行为相同
【问题讨论】:
-
请注意,
t[2]=t[2]+[1,2]会引发,但不会修改元组
标签: python list python-2.7 python-3.x tuples