我正在尝试修改元组中的列表,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)
给对象机会就地改变对象。
- 它重新绑定(bind)对
obj.__iadd__(rhs)
的引用调用返回。
通过使用 +=
在存储在元组中的列表上,第一步成功; t[0]
列表已就地更改,但第二步,重新绑定(bind) t[0]
到 t[0].__iadd__
的返回值失败,因为元组是不可变的。
后一步需要在可变和不可变对象(immutable对象)上支持相同的运算符:
>>> 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 (modifying self
) 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]