python tuple is immutable - so why can I add elements to it
Asked Answered
K

4

12

I've been using Python for some time already and today while reading the following code snippet:

>>> a = (1,2)
>>> a += (3,4)
>>> a
(1, 2, 3, 4)

I asked myself a question: how come python tuples are immutable and I can use an += operator on them (or, more generally, why can I modify a tuple)? And I couldn't answer myself.

I get the idea of immutability, and, although they're not as popular as lists, tuples are useful in python. But being immutable and being able to modify length seems contradictory to me...

Kozak answered 25/9, 2013 at 21:44 Comment(1)
@ColonelPanic yes, a bit similar problemKozak
A
36

5 is immutable, too. When you have an immutable data structure, a += b is equivalent to a = a + b, so a new number, tuple or whatever is created.

When doing this with mutable structures, the structure is changed.

Example:

>>> tup = (1, 2, 3)
>>> id(tup)
140153476307856
>>> tup += (4, 5)
>>> id(tup)
140153479825840

See how the id changed? That means it's a different object.

Now with a list, which is mutable:

>>> lst = [1, 2, 3]
>>> id(lst)
140153476247704
>>> lst += [4, 5]
>>> id(lst)
140153476247704

The id says the same.

Anaplasty answered 25/9, 2013 at 21:46 Comment(6)
ok, so still I cannot add an element to an existing tuple (which makes it truly immutable), but a new object is created on basis of another two objects - is that right?Kozak
Just one thing is still not clear in your snippets. tuple::+= is using two tuples to create the third one, the result tuple. Seems like it;s different with list::+= - because it'd be logical if it would do the same (and create a third list). But lst + [4,5] actually makes append twice. Why doesn't list::+= follow tuple::+=?Kozak
Because a list's += is in-place. It's more "why doesn't tuple do what list does", and the answer is that "it can't".Anaplasty
I thought that += operator works the same way for each sequence type. Or at least for tuples and lists. Thank you for explaining that it's totally different :).Kozak
@tkoomzaaskz: "Why doesn't list::+= follow tuple::+=" -- good question. For list, a += b is not equivalent to a = a + b. Some might consider that confusing, but they are probably not as confused, or not as frequently confused, as people would be if list+= didn't do an in-place append. Ultimately the issue is that the "inherent" equivalence between a += b and a = a + b holds when a and b have "value semantics". But Python variables don't have value semantics, they have reference semantics. That is to say, in Python a variable refers to an object, it doesn't contain a value.Possessive
Hey @Anaplasty can you link also overridable methods for +, += in your answer? Its a very useful answer, I just think it might give new python users more info so that they can expand their knowledge on their own?Vendible
C
7

Whether += modifies the object in-place or not is up to the object. With a tuple, you aren't modifying the object, as you can see if you create another variable pointing to the same object:

>>> x = (1, 2)
>>> y = x
>>> x += (3, 4)
>>> y
(1, 2)

With mutable objects such as lists, you will see that the value changes, showing up under all its names:

>>> x = [1, 2]
>>> y = x
>>> x += [3, 4]
>>> y
[1, 2, 3, 4]
Cutlor answered 25/9, 2013 at 21:49 Comment(2)
basing on your answer, the behavior of += operator is based on mutable/immutable thing, and is in-place/not-in-place respectively. Thanks.Kozak
@tkoomzaaskz: Actually, the behavior of += is up to each type. If a type defines an __iadd__ method, Python will call that; otherwise, it will call __add__ (the same method used for +) instead. Most immutable types don't define __iadd__, so a += b is the same as a = a + b; most mutable types do define __iadd__, so it mutates them in-place.Anatomy
D
3

you are not modifying it, you created a new tuple and changed the content of the a variable

try a[0] = a[0] + 1 to see the immutability

Dumas answered 25/9, 2013 at 21:47 Comment(0)
U
1

Adding some context on top of veedrac's answer.

When using augmented assignment operators with sequences (+= or *=), the special/dunder methods __iadd__ (in-place addition) or __imult__ (in-place multiplication) will be called respectively for += or *=. Those methods are implemented for list but not for tuple. If those methods are not implemented Python will fall back on the __add__ or __mult__ which both return a new object.

Those are the dunder methods being called when directly calling the + or * operator on list or tuple. (l3 = l1 + l2 where l1 and l2 are lists or t2 = t1 * 2 for t2 being a tuple)

This explains the difference of behavior between:

  1. augmented assignment operators on tuple
>>> tup = (1, 2, 3)
>>> id(tup)
140153476307856
>>> tup += (4, 5)
>>> id(tup)
140153479825840
  1. augmented assignment operators on list
>>> lst = [1, 2, 3]
>>> id(lst)
140153476247704
>>> lst += [4, 5]
>>> id(lst)
140153476247704

Please note that using those operations on tuple in a loop is inefficient because the interpreter has to copy whole target object first before doing the concatenation and returning a new object, which isn't the case when the operation is done in-place.

import time

start_time = time.time()
l1 = [1, 2, 3]
l2 = [4, 5]

for _ in range(100000):
    l1 += l2
print("--- list:  %s seconds ---" % (time.time() - start_time))

start_time = time.time()
t1 = (1, 2, 3)
t2 = (4, 5)

for _ in range(100000):
    t1 += t2
print("--- tuple:  %s seconds ---" % (time.time() - start_time))

gives as output:

--- list:  0.0055124759674072266 seconds ---
--- tuple:  20.920572996139526 seconds ---
Underlay answered 29/1, 2023 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.