实践
1、
python
s="abc" s+="34" # OK print(s) s[0]="k" # TypeError: 'str' object does not support item assignmentgolang
s := "abc" s += "456" fmt.Println(s) s[0] = "A" // cannot assign to s[0] (value of type byte)compiler UnassignableOperand typesinternal package - golang.org/x/tools/internal/typesinternal - Go Packages https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#UnassignableOperand// UnassignableOperand occurs when the left-hand side of an assignment is // not assignable. // // Example: // func f() { // const c = 1 // c = 2 // } UnassignableOperand
Design and History FAQ — Python 3.11.3 documentation https://docs.python.org/3/faq/design.html#why-are-python-strings-immutable
为什么Python字符串是不可变的?
有几个优点。
一个是性能:知道字符串是不可变的,意味着我们可以在创建时为它分配空间,并且存储需求是固定不变的。这也是元组和列表之间区别的原因之一。
另一个优点是,Python 中的字符串被视为与数字一样“基本”。 任何动作都不会将值 8 更改为其他值,在 Python 中,任何动作都不会将字符串 "8" 更改为其他值。
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.
翻译
搜索
复制
标签:typesinternal,Python,immutable,UnassignableOperand,字符串,strings From: https://www.cnblogs.com/papering/p/17392210.html