5

Why (not how) python primitive data types like int and string are immutable. Is that because of a implementation limitation of scripting language.

as a example

a = 5;
a = 6; 

in second line(a = 6;) instead of creating a new memory location, why cant it change the first memory location to 6

Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • 3
    So **why** do you need to do this? Python names are *not memory locations*. They are references to objects on a heap instead. That's just how the language works. – Martijn Pieters Apr 03 '15 at 11:28
  • 3
    I disagree with the downvoters and closevoters. The question is clear, and has an unambiguous answer. – orlp Apr 03 '15 at 11:31
  • @ Martijn Pieters: isn't it costly to declare new memory than, change the existing memory – Nayana Adassuriya Apr 03 '15 at 11:31
  • Why would you want mutable numbers? If I assign a value by taking one from another one, I don't want the original one to change. There is a general trend away from mutability in general (where possible) because it makes it harder to reason around a program (especially in multi-threaded environments). Traditional maths doesn't have mutable values, so having them would mean twisting your world view. – Gareth Latty Apr 03 '15 at 11:32
  • 3
    Immutable objects doesn't mean you are creating new ones - quite the opposite. Because they are immutable, you can share the instances (which Python does with small integers). All you are doing is changing the reference to point to something different. – Gareth Latty Apr 03 '15 at 11:33
  • @orlp: it is not showing research effort. Nor is it motivating the reasons why the OP needs this; this smells like a huge X-Y problem. *I want my wheels to be triangular rather than round*. That's rather a huge deviation from the norm, why do you need that? – Martijn Pieters Apr 03 '15 at 11:34
  • @NayanaAdassuriya: that cost is relative; there is also development effort, debugging effort, and work-arounds. CPython interns small integers (turning them into singletons at the interpreter level), for example. For *some* operations CPython internally treats strings as mutable to avoid constant replacing when appending in a loop. Etc. – Martijn Pieters Apr 03 '15 at 11:36
  • 6
    @MartijnPieters Last time I checked there was no obligation for a question to come with a motivation letter. And I don't think OP wants to change the behavior Python has, I think he wants to understand why Python was designed this way. – orlp Apr 03 '15 at 11:37
  • @MartijnPieters I can understand considering downvoting for a lack of research effort, but closing as 'unclear what you're asking' is outrageous. – orlp Apr 03 '15 at 11:38
  • @orlp: No, but it gives us a *map location*; where is the OP standing? What do they already know? Otherwise this becomes a long exercise in pin the tail on the donkey as we try and find out what exactly needs to be explained. As it stands this could be a dupe of [How do I pass a variable by reference?](http://stackoverflow.com/q/986006) for example. – Martijn Pieters Apr 03 '15 at 11:38
  • @ Martijn Pieters: As I understand immutable means always it create a new memory location and change reference to that memory. So languages like C++ not motivate to create new memory always because it is a costly process. So I needed to know what is the motivation behind that Python always do that even though there are no any other reference to the original memory location. I think you miss judge my question. – Nayana Adassuriya Apr 03 '15 at 11:39
  • @orlp: the volume of questions posted daily on Stack Overflow doesn't afford us all that much time to hand-hold each and every question asker to that extend. That's why up-front effort is *appreciated* and asked for in comments rather than going on the path-finding mission by editing and re-editing answers. That's also why there is a tool-tip on that downvote to suggest why you'd want to use it. – Martijn Pieters Apr 03 '15 at 11:40
  • @orlp: and I didn't (yet) vote to close this post. Currently I am leaning towards *too broad* because of the lack of context. – Martijn Pieters Apr 03 '15 at 11:41
  • 3
    @MartijnPieters What you're proposing is directly opposed to how SO should work in my opinion. It's not a personal 'figure-out-what-OP-wants-to-know' mission. You should just answer the question as it stands, and explain what the question asks. If that doesn't answer what the OP really wanted to know, he can open a new question. As it stands, this question can be answered by explaining why Guido chose for pointer semantics in Python. – orlp Apr 03 '15 at 11:43
  • @orlp: I am not going to do that, for two reasons: this is not a practical programming problem, it is asking about language design motivations instead, as far as I can tell. And the question has too many interpretations to be fruitfully answered in a way that is useful to both the OP and future visitors. – Martijn Pieters Apr 03 '15 at 11:48
  • @Nayana: In your example, it might be more efficient to just change the data at the memory location. But since Python is typed, but dynamically typed, you could change the type of a. E.g. First `a==5`, followed by `a=="Hello, world!"`. Here you must allocate new memory, and/or change the reference to point to another place in memory. Filling 4/8 bytes with an arbitrarily long string (or arbitrarily complex objects for that matter) is not possible. What you are asking for would indeed be a specialized optimization for a corner case, which might or might not pay off. – cfi Apr 03 '15 at 11:58
  • (And actually integers are not 4 or 8 bytes in Python. So forgive inaccuracies in my example. It's supposed to just be an illustration..) – cfi Apr 03 '15 at 11:59
  • @Nayana: this is a matter of implementation, lua for example has also immutable string and number types but saves the actual number on a stack. One might do the same in a special python implementation. – Daniel Apr 03 '15 at 12:05

1 Answers1

8

Some Python data types are immutable because Python uses reference/pointer semantics.

This means that whenever you assign an expression to a variable, you're not actually copying the value into a memory location denoted by that variable, but you're merely giving a name to the memory location where the value actually exists.

Now, if for example strings were mutable, this would happen:

a = "test"
b = a
b[2] = "o"

# Now a would be "tost", oops.

This behaviour was considered unintuitive, so strings were made immutable.


Similarly for integers, if assigning a new value would change the original location, the following would happen:

a = 5
b = a
b += 5

# a is now 10 :(
orlp
  • 112,504
  • 36
  • 218
  • 315