Python Tuples Immutability Busted

What are Tuples in Python.

By definition Tuples are immutable data types which store a collection of objects.

Creating Tuples

Accessing Tuples

Updating Tuples

Tuples are immutable but potential changeable.

Python tuples have a surprising trait: they are immutable, but their values may change. This may happen when a tuple holds a reference to any mutable object, such as a list. To understand this a good first step is to challenge the common notion that variables are like boxes where you store data.

Python variables are like reference variables in Java, so it’s better to think of them as labels attached to objects.

Python variables are Name alias

Here is a Tweedledum’s and Tweedledee’s twins example inspired by Lewis Carroll’s:

It’s clear that dum and dee refer to objects that are equal, but not to the same object. They have distinct identities.

Tweedledum decided to become a rapper, adopting the stage name T-Doom. This is how we can express this in Python:

So, t_doom and dum are equal — but Alice might complain that it’s foolish to say that, because t_doom and dum refer to the same person: t_doom is dum.

The names t_doom and dum are aliases. The official Python docs often refer to variables as “names”. Variables are names we give to objects. Alternate names are aliases. That helps freeing our mind from the idea that variables are like boxes.

After much practice, T-Doom is now a skilled rapper

T-Doom acquired the ‘rap’ skill, and so did Tweedledum — of course, they are one and the same. If t_doom was a box containing a str and a list, how could you explain that appending to the list in t_doom also changes the list in the dum box? However, it makes perfect sense if you see variables as labels.

The label analogy is much better because aliasing is explained simply as an object with two or more labels. In the example, t_doom[1] and skills are two names given to the same list object, just as dum and t_doom are two names given to the same tuple object.

What is immutable is the physical content of a tuple, consisting of the object references only. The value of the list referenced by dum[1] changed, but the referenced object id is still the same. A tuple has no way of preventing changes to the values of its items, which are independent objects and may be reached through references outside of the tuple, like the skills name we used earlier. Lists and other mutable objects inside tuples may change, but their ids will always be the same.

After dum became a rapper, the twin brothers are no longer equal:

We have two tuples that were created equal, but now they are different.

The other built-in immutable collection type in Python, frozenset, does not suffer from the problem of being immutable yet potentially changing in value. That’s because a frozenset (or a plain set, for that matter) may only hold references to hashable objects, and the value of hashable objects may never change, by definition.

Tuples are commonly used as dict keys, and those must be hashable — just as set elements. So, are tuples hashable or not? The right answer is: some tuples are hashable. The value of a tuple holding a mutable object may change, and such a tuple is not hashable. To be used as a dict key or set element, the tuple must be made only of hashable objects.

Our tuples named dum and dee are unhashable because each contains a list reference, and lists are unhashable.

Conclusion

Thanks for reading. If you still have questions, feel free to reach out either in the comments section or on email.

Reference

http://radar.oreilly.com/2014/10/python-tuples-immutable-but-potentially-changing.html

Software Developer during day time, blogger at night.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store