相关疑难解决方法(0)

为什么+ = Python元组中的列表会引发TypeError但是仍然会修改列表?

我刚看到一些非常奇怪的东西.

>>> t = ([],)
>>> t[0].append('hello')
>>> t
(['hello'],)
>>> t[0] += ['world']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> t
(['hello', 'world'],)
Run Code Online (Sandbox Code Playgroud)

为什么它会提升TypeError并改变list内部tuple

python

16
推荐指数
2
解决办法
770
查看次数

标签 统计

python ×1