0

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.

Zenquiorra
  • 140
  • 1
  • 2
  • 13
  • 1
    Does this answer your question? [How is tuple implemented in CPython?](https://stackoverflow.com/questions/14135542/how-is-tuple-implemented-in-cpython) – Kent Shikama Dec 22 '19 at 11:14
  • @KentShikama No, I have edited my post. Thanks – Zenquiorra Dec 22 '19 at 11:23
  • Can anyone explain why is there a downvote on this question? I will modify it if it's unclear. – Zenquiorra Dec 22 '19 at 11:24
  • "with respect to Python itself directly" - What do you mean by this? CPython is the reference implementation of Python. I could create my own Python implementation that uses `[insert crazy data structure here]` to implement tuples but I'm assuming you're not interested in that. – Kent Shikama Dec 22 '19 at 11:26

1 Answers1

7

There's nothing special about the implementation or memory layout of tuples that makes them immutable. They're immutable because they just don't have any mutator operations.

Lists are mutable because the list class implements operations like append and __setitem__ that mutate lists. Such operations have to be deliberately included in the implementation; they don't come into existence automatically. If list didn't have mutator operations, lists would be immutable too.

At the level of the C implementation, the C code that implements tuples has to be able to write to a tuple's memory, and at that level, the data structures are mutable. The interface the implementation presents to Python code is immutable, though.

You can't do the same with a class you implement in Python because you can't get a firm separation between implementation and interface. Such a separation arises automatically when implementing a Python class in C due to the design of the Python C API, but without anything like a private access modifier, you can't do the same in Python.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 1
    Ok, that made sense! It is just how it is defined in its implementation, there are no `mutator operations` defined on them so we cannot use them, which makes a tuple immutable. Alright! Also, it means that if I want to create an immutable class of my own in Python then that's not possible. I have to use an existing immutable data structure to do that. – Zenquiorra Dec 22 '19 at 11:40