参见英文答案 > a mutable type inside an immutable container 3个
我正在尝试修改元组中的列表,append方法可以正常工作,而=运算符工作但引发了异常,说无法修改元组.我知道一个元组是不可变的,但我不是想改变它.为什么会这样?
In [36]: t=([1,2],)
In [37]: t[0].append(123)
In [38]: t
Out[38]: ([1, 2, 123],)
In [39]: t[0]+=[4,5,]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-39-b5b3001fbe03> in <module>()
----> 1 t[0]+=[4,5,]
TypeError: 'tuple' object does not support item assignment
In [40]: t
Out[40]: ([1, 2, 123, 4, 5],)
解决方法:
=是就地加法运算符.它做了两件事:
>它调用obj .__ iadd __(rhs)来给对象提供就地变异对象的机会.
>它重新引用obj .__ iadd __(rhs)调用返回的引用.
通过在存储在元组中的列表上使用=,第一步成功; t [0]列表就地改变,但第二步,将t [0]重新绑定到t [0] .__ iadd__的返回值失败,因为元组是不可变的.
需要后一步来支持可变和不可变对象上的相同运算符:
>>> reference = somestr = 'Hello'
>>> somestr += ' world!'
>>> somestr
'Hello world!'
>>> reference
'Hello'
>>> reference is somestr
False
这里添加了一个不可变的字符串,somestr被反弹到一个新对象,因为字符串是不可变的.
>>> reference = somelst = ['foo']
>>> somelst += ['bar']
>>> somelst
['foo', 'bar']
>>> reference
['foo', 'bar']
>>> reference is somestr
True
这里列表已就地更改,somestr被反弹到同一个对象,因为list .__ iadd __()可以就地改变列表对象.
从augmented arithmetic special method hooks documentation:
These methods are called to implement the augmented arithmetic assignments (
+=
,-=
,*=
,/=
,//=
,%=
,**=
,<<=
,>>=
,&=
,^=
,|=
). These methods should attempt to do the operation in-place (modifyingself
) and return the result (which could be, but does not have to be,self
).
这里的解决方法是调用t [0] .extend()代替:
>>> t = ([1,2],)
>>> t[0].extend([3, 4, 5])
>>> t[0]
[1, 2, 3, 4, 5]