【发布时间】:2012-02-28 15:29:09
【问题描述】:
我对修改元组成员有点困惑。以下不起作用:
>>> thing = (['a'],)
>>> thing[0] = ['b']
TypeError: 'tuple' object does not support item assignment
>>> thing
(['a'],)
但这确实有效:
>>> thing[0][0] = 'b'
>>> thing
(['b'],)
同样有效:
>>> thing[0].append('c')
>>> thing
(['b', 'c'],)
不工作,但工作(嗯?!):
>>> thing[0] += 'd'
TypeError: 'tuple' object does not support item assignment
>>> thing
(['b', 'c', 'd'],)
看起来和以前的一样,但是有效:
>>> e = thing[0]
>>> e += 'e'
>>> thing
(['b', 'c', 'd', 'e'],)
那么游戏的规则到底是什么,什么时候可以修改元组内的东西,什么时候不能修改?这似乎更像是禁止对元组成员使用赋值运算符,但最后两种情况让我感到困惑。
【问题讨论】:
-
注意:现在在Python FAQ
标签: python list tuples immutability mutable