Tuples and immutability

Ian Kelly ian.g.kelly at gmail.com
Sun Mar 2 13:32:35 CET 2014

On Sat, Mar 1, 2014 at 7:04 PM, Eric Jacoboni <eric.jacoboni at gmail.com> wrote:
> In fact, i think i'm gonna forget += on lists :)

Well, do what you want, but I think you're taking the wrong lesson
from this.  Don't forget about using += on lists.  Instead, forget
about using assignments, augmented or otherwise, on tuple elements.
Would you expect this to work?

    tup = (1, 2, 3)
    tup[1] += 42

I'm guessing probably not.  So I have a hard time understanding why it
should be expected to work any differently when the tuple element
happens to be a list.  The fact that the list is actually modified I
think is a distraction from the main point: the error indicates that
something illegal happened, not that nothing has changed.  Assignments
just are not allowed on tuple elements, and it doesn't matter what
type they happen to be.

More information about the Python-list mailing list