为什么Python中的string不可变

Why are Python strings immutable?

There are several advantages.

  • One is performance: knowing that a string is immutable means we can allocate space for it at creation time, and the storage requirements are fixed and unchanging. This is also one of the reasons for the distinction between tuples and lists.

  • Another advantage is that strings in Python are considered as “elemental” as numbers. No amount of activity will change the value 8 to anything else, and in Python, no amount of activity will change the string “eight” to anything else.

大白话就是:

  1. 固定的内存空间更有效率

  2. 就像没有人会改变数值8的写法一样,字符串“eight”也不需要改变。

发表评论

电子邮件地址不会被公开。 必填项已用*标注