Tuples and immutability

Eric Jacoboni eric.jacoboni at gmail.com
Sun Mar 2 03:04:32 CET 2014

Le 01/03/2014 22:21, Mark H. Harris a écrit :
> The point I'm trying to make with this post is that  s[2]+=[46]   and
> s[2]=s[2]+[46]  are handled inconsistently.  

For my own, the fact that, in Python, a_liste += e_elt gives a different
result than a_list = a_list + e_elt is a big source of trouble...
I don't really find a good reason to justify that : a_list += "spam"
gives a valid result, when a_list = a_list + "spam" is not allowed. Why
the former is like a_list.extend() in the first place ? (well, it's not
exactly an extend() as extend returns None, not +=).

And if both operations were equivalent, i think my initial question
about tuples will be clear and the behavio(u)r would me more consistent
for me :

tu = (10, [10, 30])
tu[1] = tu[1] + [20]                    <-- Big mistake!
TypeError: 'tuple' object does not support item assignment  <-- ok
(10, [10, 30])                          <-- ok too...

In fact, i think i'm gonna forget += on lists :)

More information about the Python-list mailing list