Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Python strings immutable? Best practices for using them

  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?
like image 368
Sergey Avatar asked Dec 30 '11 13:12

Sergey


People also ask

Why are strings immutable in python?

In Python, strings are made immutable so that programmers cannot alter the contents of the object (even by mistake). This avoids unnecessary bugs. Some other immutable objects are integer, float, tuple, and bool. More on mutable and immutable objects in Python.

What are the benefits of strings being immutable?

Immutable strings are cheap to copy, because you don't need to copy all the data - just copy a reference or pointer to the data. Immutable classes of any kind are easier to work with in multiple threads, the only synchronization needed is for destruction.

Why are strings immutable programming?

Having strings as immutable also allows the new string references easy, as the same/similar strings will be readily available from the pool of the Strings previously created. Thereby reducing the cost of new object creation.

Why strings are immutable with an example?

The String is immutable in Java because of the security, synchronization and concurrency, caching, and class loading. The reason of making string final is to destroy the immutability and to not allow others to extend it. The String objects are cached in the String pool, and it makes the String immutable.


2 Answers

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:] 
like image 185
Fred Foo Avatar answered Sep 23 '22 19:09

Fred Foo


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.

like image 45
S.Lott Avatar answered Sep 23 '22 19:09

S.Lott