- #1
Grinkle
Gold Member
- 819
- 234
- TL;DR Summary
- Is it accurate to say that strings are immutable in Python?
I am using "Introducing Python" (Lubanovic) to teach myself Python.
I read that Python strings are immutable, then I see code like this -
string1 = 'base_val'
string1= 'new_val'
not throw up an error. A little investigation shows me that the interpreter created two strings and string1 is a pointer. This is all fine, and it has performance implications and it is important to understand, I get all that. 'base_val' was not necessarily destroyed when I changed the value of string1, the interpreter created a new string called 'new_val' and changed what string1 points to, and the literal 'base_val' was never changed.
Integers, on the other hand, are described as mutable. (edit: My mistake, the book correctly says that ints are also not mutable).
int1 = 3
int1 = 4
If Python were my first programming language, I'd be very confused about what 'immutability' is meant to imply. It seems to me that the immutability of Python strings is true in the context of the Python interpreter implementation code, not at all true in the context of a Python author's Python source code. There is no conceptual difference between the int code and string code above. At the level of Python functionality (which I distinguish from Python interpreter implementation) strings are mutable.
I looked on the internet to see if the author of my book is alone in describing Python strings as immutable, and he is not (I expected to find that, just noting so that no one thinks I am complaining about Lubanovic, I am not).
So, I think I am missing something - I am a couple decades past thinking I am smarter than the Python community after a couple days of self study. Can someone help me with a more pragmatic way of interpreting the statement that Python strings are immutable (rather than me dismissing this as not really relevant and a Python interpreter implementation thing, good to know but not really part of Python per se).
I do realize without surprise that some string modifications need to use string-object access functions and cannot be done directly, eg -
string1[2] = 'x'
throws and errror, I'd need to use string1.replace() after having found where in the string I want the x for instance, and for these kinds of string modifications there is a conceptual difference between changing an int and changing a string.
This is a part of Python per se, but to my thinking, it is how one changes strings in Python, which one can only do if strings are mutable.
Perhaps this is all about how to pronounce tomato, but I would really like to be more on the same page with the mainstream Python description of strings being immutable.
I read that Python strings are immutable, then I see code like this -
string1 = 'base_val'
string1= 'new_val'
not throw up an error. A little investigation shows me that the interpreter created two strings and string1 is a pointer. This is all fine, and it has performance implications and it is important to understand, I get all that. 'base_val' was not necessarily destroyed when I changed the value of string1, the interpreter created a new string called 'new_val' and changed what string1 points to, and the literal 'base_val' was never changed.
Integers, on the other hand, are described as mutable. (edit: My mistake, the book correctly says that ints are also not mutable).
int1 = 3
int1 = 4
If Python were my first programming language, I'd be very confused about what 'immutability' is meant to imply. It seems to me that the immutability of Python strings is true in the context of the Python interpreter implementation code, not at all true in the context of a Python author's Python source code. There is no conceptual difference between the int code and string code above. At the level of Python functionality (which I distinguish from Python interpreter implementation) strings are mutable.
I looked on the internet to see if the author of my book is alone in describing Python strings as immutable, and he is not (I expected to find that, just noting so that no one thinks I am complaining about Lubanovic, I am not).
So, I think I am missing something - I am a couple decades past thinking I am smarter than the Python community after a couple days of self study. Can someone help me with a more pragmatic way of interpreting the statement that Python strings are immutable (rather than me dismissing this as not really relevant and a Python interpreter implementation thing, good to know but not really part of Python per se).
I do realize without surprise that some string modifications need to use string-object access functions and cannot be done directly, eg -
string1[2] = 'x'
throws and errror, I'd need to use string1.replace() after having found where in the string I want the x for instance, and for these kinds of string modifications there is a conceptual difference between changing an int and changing a string.
This is a part of Python per se, but to my thinking, it is how one changes strings in Python, which one can only do if strings are mutable.
Perhaps this is all about how to pronounce tomato, but I would really like to be more on the same page with the mainstream Python description of strings being immutable.
Last edited: