Tuples and immutability

Ian Kelly ian.g.kelly at gmail.com
Thu Feb 27 17:18:01 EST 2014


On Thu, Feb 27, 2014 at 2:47 PM, Nick Timkovich <prometheus235 at gmail.com> wrote:
> On Thu, Feb 27, 2014 at 10:33 AM, Chris Angelico <rosuav at gmail.com> wrote:
>>
>> On Fri, Feb 28, 2014 at 3:27 AM, Eric Jacoboni <eric.jacoboni at gmail.com>
>> wrote:
>> > But, imho, it's far from being a intuitive result, to say the least.
>>
>> It's unintuitive, but it's a consequence of the way += is defined. If
>> you don't want assignment, don't use assignment :)
>>
>> ChrisA
>
>
> Where is `.__iadd__()` called outside of `list += X`?  If the only
> difference from `.extend()` is that it returns `self`, but the list was
> already modified anyway, why bother with reassignment?

x += y is meant to be equivalent, except possibly in-place and more
efficient, than x = x + y.  If you skip the assignment, and that
assignment is meaningful to whatever the left side may be (e.g.
assigning to a descriptor or something that invokes __setitem__ or
__setattr__), then the operation is not equivalent.



More information about the Python-list mailing list