8.1 Tuples and named tuples
Tuples are objects that can store a specific number of other objects in sequence. A collection of related data items is one important aspect of class design. A tuple is immutable, meaning we can’t add, remove, or replace objects after a tuple has been created. This may seem like a massive restriction, but the truth is, if you need to modify a tuple, you’re using the wrong data type (usually, a list type would be more suitable). The primary benefit of tuples’ immutability is a tuple of immutable objects (such as strings and numbers and other tuples) has a hash value, allowing us to use them as keys in dictionaries, and members of a set.
A tuple that contains a mutable structure, such as a list, set, or dict, isn’t composed of immutable items, and doesn’t have a hash value. We’ll look closely at this distinction in the next section.
We can use Python’s built-in generic tuple class to store...