python元组不支持_python – 获取“’元组’对象的列表的变量元组不支持项目分配”...

参见英文答案 > 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)

in ()

----> 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 __()可以就地改变列表对象.

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]

标签:python,tuples,immutability,list

来源: https://codeday.me/bug/20190714/1459856.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值