Immutability

Immutable objects can't be changed in place; without creation of a new variable, you have to create a new object. In of itself, this is inconvenient, but it also implies that none of the internal values can be changed, which—by definition—means that tuples cannot contain lists or any other dynamic objects.

They are made like this on purpose, and this has serious merits. First, the dynamic nature of lists comes with a price: in order to be both dynamic and highly performant, they reserve more memory than they need. Tuples, by comparison, use up to two times less memory! Secondly, and this is the most important part, as tuples are guaranteed to be static, they can be used as an immutable part of other data structures, such as sets or dictionaries. Essentially, once we guarantee that the value cannot change, we can use it to generate its unique ID—hash—and to retrieve either itself or other data, using this hash as a key.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset