72
  1. What are the design reasons of making Python strings immutable? How does it make programming easier?
  2. I'm used to mutable strings, like the ones in C. How am I supposed to program without mutable strings? Are there any best practices?
3
  • 2
    There is a mutable string type, it is called bytearray Commented Dec 30, 2011 at 18:01
  • 7
    @JanneKarila: The semantics, however, are wildly different. It doesn't, for example, handle Unicode characters, just bytes. It's not a proper string -- with string methods -- it's an adjustable array of bytes. Commented Dec 30, 2011 at 21:24
  • @JanneKarila but is it a C-string ? Commented Oct 15, 2020 at 6:29

7 Answers 7

62

When you receive a string, you'll be sure that it stays the same. Suppose that you'd construct a Foo as below with a string argument, and would then modify the string; then the Foo's name would suddenly change:

class Foo(object):
    def __init__(self, name):
        self.name = name

name = "Hello"
foo = Foo(name)
name[0] = "J"

With mutable strings, you'd have to make copies all the time to prevent bad things from happening.

It also allows the convenience that a single character is no different from a string of length one, so all string operators apply to characters as well.

And lastly, if strings weren't immutable, you couldn't reliably use them as keys in a dict, since their hash value might suddenly change.

As for programming with immutable strings, just get used to treating them the same way you treat numbers: as values, not as objects. Changing the first letter of name would be

name = "J" + name[1:]
Sign up to request clarification or add additional context in comments.

4 Comments

Sooooooo I know this is a bit late, but could you explain what you mean by mutable strings not being reliable in dicts? List are mutable, but can be put in dicts, what would make mutable strings 'unreliable'?
@wnnmaw What larsmans means is that you wouldn't be able to reliably use strings as keys to a dict, since modifying a string would change its hash, which would change the bucket the string would go to. You can see this with lists yourself by trying to use a list as a key to a dictionary (a = {}; a[[1,2]] = 1 throws a TypeError).
But its easy to change strings: str = list(str); str[0] = "J"; str = ''.join(str)
I don't get it. What bad consequences could happen if you replace the first letter of name? Isn't it same if I assign a totally new value to name in this case?
18

Immutable strings greatly simplify memory allocation when compared with C strings: you don't guess at a length and over-allocate hoping you over-allocated enough.

They're more secure: you can never have a buffer overrun the way you can in C.

There is only one mutable string use case.

  • replacing a substring or a single character

All other string use cases (concatenation, searching, etc., etc.) the mutability does not matter. In all other cases, mutability does not matter.

If you want to replace a character or a substring in Python, you simply create a new string

x = x[:place] + replacement + x[place+1:]

That's the only code that novel or distinctive.


For reasons I fail to understand, it appears important to add the following.

"There are other ways to avoid a string buffer overflow than immutable strings."

For the purposes of this question (about Python, specifically) immutable strings have a pleasant consequence of no buffer overflows. For other languages, other principles, rules and nuances apply.

24 Comments

Ruby (as one example) has mutable strings, and it has automatic memory management and can't have buffer overflows. As for replacing substrings being the only use case, what about (in-place-)appending? -1
You made (before the edit 31 minutes ago) a far more general (and, due to that generality, wrong) statement, to which I provided a counter-example. The part of the question you addressed in that part of your answer ("why make strings immutable?") didn't speak of C. And in-place appending is different from concatenating two immutable strings and putting the result back into a variable - the difference is plainly visible to all code that accesses the (old) string (instead of going through the updated variable).
I am talking about Python, but to contrast mutable with immutable strings (part of the question and all answers, mind you), I provided an example. This example couldn't have been in Python as it has immutable strings only. In particular, in-place appending (not the contracted form of s = s + ... but the operation that modifies the string object) does not exist in Python. That's the point I'm trying to make. Your answer is incomplete regarding operations possible with mutable strings but not possible with immutable strings.
I have already explained why I believe my comment is as on-topic as your answer. But I'll try again, this time starting from axioms. (1) The question asks "What are the design reasons of making Python strings immutable?", so (2) a comparision of Python's (immutable) strings to mutable strings is in order. (3) We agree on that, your answer does such a comparision. (4) Such a comparision must necessarily consider mutable strings, and thus (5) a language feature that is not included in Python. (6) You do so, choosing C's mutable strings as contrast. Are these observations correct?
@DavidHeffernan: Things you "could" do don't seem to be relevant to this question. I thought this was a question about immutable Python strings. Again, the context switch has me confused. I guess I should just give up trying to understand the comments.
|
11
  1. Immutable objects are automatically threadsafe.
  2. You will find that using Python strings is trivially easy in comparison to the extreme pain associated with strings in C.

2 Comments

The painfulness of C strings has reasons that have little to do with being mutable. That merely exacerbates the problem a bit.
@leftaround I agree, but I didn't say that painfulness of C strings has anything to do with them being mutable
9

Immutable strings can be keys in dictionaries and similar data structures, without the need to copy the strings. It is easier to make a mutable wrapper around an immutable string than the other way around.

Comments

4

Most languages have immutable strings. This includes Java, Python, and C#. Usually when concatenating strings, the language allocates an entirely new string and copies the content of the two strings into the new string.

Immutability does tend to make programming easier. Especially when dealing with a multi-threaded environment.

Comments

3

Immutable strings makes programming much easier, which is why C# and Java use them too.

Had strings been mutable, you would not be able to trust any externally-provided string, since a malicious caller could change it underneath you.
It would also make multi-threading much more difficult.

5 Comments

Can you give anexample?
An example of what?
"you would not be able to trust any externally-provided string" for this. Thanks
You write a function that accepts a string, but your caller mutates the string while your function is running.
Isn't that good sometimes? I will have a data holder which has the latest changes in it.
1

Immutable strings are much more dangerous than mutable strings in certain contexts. A best practice list should include never temporarily storing passwords or keys as strings in python.

While it is great that immutable strings are safer in string operations and offer other consistency advantages, storing (even temporarily) a password or goodness forbid a key: This puts that value in memory for the life of that program. This may end up inside a core dump file (that is improperly permissioned). If a virtual machine running the program is paused, v-motioned to a different physical machine... this allows the data in memory in this program to leak to people outside the group of users and administrators of the solution.

Due to our massively convenient/complex virtualization stack we've built, you need to do your best to avoid accepting passwords or storing keys in memory. There is precious little information on how to do this, but it's best to follow solutions that use OS native mechanisms designed to perform these operations properly including openssl, ssh, keyring, etc.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.