I want to know how are python tuples made immutable, in the sense that how are they implemented in the memory such that we cannot change their values?
A python list in general
is implemented as a dynamic array and its values can be modified at a particular index by directly accessing the index and assigning a different value. Tuples are immutable, but what conditions make them immutable? Why can we not change their values, is it like the memory elements are locked with extra conditions over a list which makes it a tuple?
Also
A string is immutable, it makes sense why it is immutable as its state cannot be changed. A string p
will always represent p
. But what's with tuple?
Any definitions above are very general and I am not sure what extra conditions or cases python takes into consideration when I said python list is implemented as a dynamic array
.
Please suggest how the implementation works and also any sources where I can read more about this.
Edit: I already saw this thread : How is tuple implemented in CPython? But I cannot understand it properly. I need a more simplified explanation.