不可变容器内的可变类型
- 2025-03-05 09:17:00
- admin 原创
- 76
问题描述:
我对修改元组成员有点困惑。以下方法不起作用:
>>> 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'],)
那么,游戏规则到底是什么?什么时候可以修改元组内部的内容?什么时候不能修改?这似乎更像是禁止对元组成员使用赋值运算符,但最后两种情况让我感到困惑。
解决方案 1:
你总是可以修改元组中的可变值。你看到的令人费解的行为是
>>> thing[0] += 'd'
是由 引起的+=
。+=
运算符执行就地加法,但也执行赋值——就地加法仅适用于文件,但赋值会失败,因为元组是不可变的。可以把它想象成
>>> thing[0] = thing[0] + 'd'
更好地解释了这一点。我们可以使用标准库中的dis
模块来查看从两个表达式生成的字节码。+=
我们得到一个INPLACE_ADD
字节码:
>>> def f(some_list):
... some_list += ["foo"]
...
>>> dis.dis(f)
2 0 LOAD_FAST 0 (some_list)
3 LOAD_CONST 1 ('foo')
6 BUILD_LIST 1
9 INPLACE_ADD
10 STORE_FAST 0 (some_list)
13 LOAD_CONST 0 (None)
16 RETURN_VALUE
我们+
得到BINARY_ADD
:
>>> def g(some_list):
... some_list = some_list + ["foo"]
>>> dis.dis(g)
2 0 LOAD_FAST 0 (some_list)
3 LOAD_CONST 1 ('foo')
6 BUILD_LIST 1
9 BINARY_ADD
10 STORE_FAST 0 (some_list)
13 LOAD_CONST 0 (None)
16 RETURN_VALUE
STORE_FAST
请注意,我们在两个地方都得到了。这是当您尝试将其存储回元组时失败的字节码——INPLACE_ADD
紧随其后的 可以正常工作。
这解释了为什么“不起作用但起作用”的情况会留下修改后的列表:元组已经有了对该列表的引用:
>>> id(thing[0])
3074072428L
然后该列表被修改并且INPLACE_ADD
失败STORE_FAST
:
>>> thing[0] += 'd'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
因此,元组仍然引用同一个列表,但是列表已经被就地修改:
>>> id(thing[0])
3074072428L
>>> thing[0]
['b', 'c', 'd']
解决方案 2:
您无法修改元组,但可以修改元组中的内容。列表(以及集合、字典和对象)是引用类型,因此元组中的“内容”只是一个引用 - 实际列表是一个可变对象,由该引用指向,并且可以在不更改引用本身的情况下进行修改。
( + ,) <--- your tuple (this can't be changed)
|
|
v
['a'] <--- the list object your tuple references (this can be changed)
后thing[0][0] = 'b'
:
( + ,) <--- notice how the contents of this are still the same
|
|
v
['b'] <--- but the contents of this have changed
后thing[0].append('c')
:
( + ,) <--- notice how this is still the same
|
|
v
['b','c'] <--- but this has changed again
出现错误的原因+=
在于它并不完全等同于.append()
——它实际上先进行加法然后进行赋值(并且赋值失败),而不是仅仅进行就地追加。
解决方案 3:
您不能替换元组中的元素,但可以替换元素的所有内容。这将起作用:
thing[0][:] = ['b']
相关推荐
热门文章
项目管理软件有哪些?
热门标签
曾咪二维码
扫码咨询,免费领取项目管理大礼包!
云禅道AD