Python 字符串不是不可变的吗?那么为什么 + ” ” +工作?

发布于 2025-01-01 00:59:22 字数 314 浏览 2 评论 0原文

我的理解是 Python 字符串是不可变的。

我尝试了以下代码:

a = "Dog"
b = "eats"
c = "treats"

print a, b, c
# Dog eats treats

print a + " " + b + " " + c
# Dog eats treats

print a
# Dog

a = a + " " + b + " " + c
print a
# Dog eats treats
# !!!

Python 不应该阻止分配吗?我可能错过了一些东西。

有什么想法吗?

My understanding was that Python strings are immutable.

I tried the following code:

a = "Dog"
b = "eats"
c = "treats"

print a, b, c
# Dog eats treats

print a + " " + b + " " + c
# Dog eats treats

print a
# Dog

a = a + " " + b + " " + c
print a
# Dog eats treats
# !!!

Shouldn't Python have prevented the assignment? I am probably missing something.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(22

向地狱狂奔 2025-01-08 00:59:22

首先a指向字符串“Dog”。然后,您将变量 a 更改为指向新字符串“Dog eats treats”。您实际上并没有改变字符串“Dog”。字符串是不可变的,变量可以指向它们想要的任何内容。

First a pointed to the string "Dog". Then you changed the variable a to point at a new string "Dog eats treats". You didn't actually mutate the string "Dog". Strings are immutable, variables can point at whatever they want.

眼藏柔 2025-01-08 00:59:22

字符串对象本身是不可变的。

指向字符串的变量 a 是可变的。

考虑:

a = "Foo"
# a now points to "Foo"
b = a
# b points to the same "Foo" that a points to
a = a + a
# a points to the new string "FooFoo", but b still points to the old "Foo"

print a
print b
# Outputs:

# FooFoo
# Foo

# Observe that b hasn't changed, even though a has.

The string objects themselves are immutable.

The variable, a, which points to the string, is mutable.

Consider:

a = "Foo"
# a now points to "Foo"
b = a
# b points to the same "Foo" that a points to
a = a + a
# a points to the new string "FooFoo", but b still points to the old "Foo"

print a
print b
# Outputs:

# FooFoo
# Foo

# Observe that b hasn't changed, even though a has.
小巷里的女流氓 2025-01-08 00:59:22

变量 a 指向对象“Dog”。最好将 Python 中的变量视为标签。您可以将标签移动到不同的对象,这就是您将 a = "dog" 更改为 a = "dog eats treats" 时所做的操作。

然而,不变性是指对象,而不是标签。


如果您尝试 a[1] = 'z'"dog" 转换为 "dzg",您会收到错误:

TypeError: 'str' object does not support item assignment" 

因为字符串不支持项目分配,因此它们是不可变的。

The variable a is pointing at the object "Dog". It's best to think of the variable in Python as a tag. You can move the tag to different objects which is what you did when you changed a = "dog" to a = "dog eats treats".

However, immutability refers to the object, not the tag.


If you tried a[1] = 'z' to make "dog" into "dzg", you would get the error:

TypeError: 'str' object does not support item assignment" 

because strings don't support item assignment, thus they are immutable.

幽梦紫曦~ 2025-01-08 00:59:22

仅当我们能够更改内存位置中保存的值而不更改内存位置本身时,某些东西才是可变的。

诀窍是:如果发现改变前后的内存位置相同,则它是可变的。

例如,列表是可变的。如何?

>> a = ['hello']
>> id(a)
139767295067632

# Now let's modify
#1
>> a[0] = "hello new"
>> a
['hello new']
Now that we have changed "a", let's see the location of a
>> id(a)
139767295067632
so it is the same as before. So we mutated a. So list is mutable.

字符串是不可变的。我们如何证明这一点?

> a = "hello"
> a[0]
'h'
# Now let's modify it
> a[0] = 'n'
----------------------------------------------------------------------

我们得到

TypeError:“str”对象不支持项目分配

因此我们未能改变字符串。这意味着字符串是不可变的。

在重新分配时,您将变量更改为指向新位置本身。这里你没有改变字符串,而是改变变量本身。以下是您正在做的事情。

>> a = "hello"
>> id(a)
139767308749440
>> a ="world"
>> id(a)
139767293625808

重新分配前后的id是不同的,所以这证明你实际上没有改变,而是将变量指向新的位置。这不是改变该字符串,而是改变该变量。

Something is mutable only when we are able to change the values held in the memory location without changing the memory location itself.

The trick is: If you find that the memory location before and after the change are the same, it is mutable.

For example, list is mutable. How?

>> a = ['hello']
>> id(a)
139767295067632

# Now let's modify
#1
>> a[0] = "hello new"
>> a
['hello new']
Now that we have changed "a", let's see the location of a
>> id(a)
139767295067632
so it is the same as before. So we mutated a. So list is mutable.

A string is immutable. How do we prove it?

> a = "hello"
> a[0]
'h'
# Now let's modify it
> a[0] = 'n'
----------------------------------------------------------------------

we get

TypeError: 'str' object does not support item assignment

So we failed mutating the string. It means a string is immutable.

In you reassigning, you change the variable to point to a new location itself. Here you have not mutated the string, but mutating the variable itself. The following is what you are doing.

>> a = "hello"
>> id(a)
139767308749440
>> a ="world"
>> id(a)
139767293625808

id before and after reassignment is different, so it this proves that you are actually not mutating, but pointing the variable to new location. Which is not mutating that string, but mutating that variable.

绮筵 2025-01-08 00:59:22

考虑:

>>> a='asdf'
>>> a.__repr__
<method-wrapper '__repr__' of str object at 0x1091aab90>
>>> a='asdf'
>>> a.__repr__
<method-wrapper '__repr__' of str object at 0x1091aab90>
>>> a='qwer'
>>> a.__repr__
<method-wrapper '__repr__' of str object at 0x109198490>

请注意,当我在变量中存储相同的值两次时,十六进制内存位置没有改变。当我存储不同的值时它确实发生了变化。该字符串是不可变的。不是因为狂热,而是因为您付出了在内存中创建新对象的性能损失。变量a只是指向该内存地址的标签。它可以改变为指向任何东西。

Consider:

>>> a='asdf'
>>> a.__repr__
<method-wrapper '__repr__' of str object at 0x1091aab90>
>>> a='asdf'
>>> a.__repr__
<method-wrapper '__repr__' of str object at 0x1091aab90>
>>> a='qwer'
>>> a.__repr__
<method-wrapper '__repr__' of str object at 0x109198490>

Notice that the hex memory location did not change when I stored the same value in the variable twice. It did change when I stored a different value. The string is immutable. Not because of zealotry, but because you pay the performance penalty of creating a new object in memory. The variable a is just a label pointing to that memory address. It can be altered to point to anything.

梦屿孤独相伴 2025-01-08 00:59:22

变量只是指向对象的标签。该对象是不可变的,但如果您愿意,您可以使标签指向完全不同的对象。

A variable is just a label pointing to an object. The object is immutable, but you can make the label point to a completely different object if you want to.

书信已泛黄 2025-01-08 00:59:22

语句a = a + " " + b + " " + c 可以根据指针进行分解。

a + " " 表示给我 a 指向的内容,该内容无法更改,并将 " " 添加到我当前的工作集。

memory:

working_set = "Dog "
a = "Dog" 
b = "eats"
c = "treats"

+ b 表示给我 b 指向的内容,该内容无法更改,并将其添加到当前工作集中。

memory:

working_set = "Dog eats"
a = "Dog" 
b = "eats"
c = "treats"

+ " " + c 表示将 " " 添加到当前集合。然后给我 c 指向的内容,该内容无法更改,并将其添加到当前工作集中。
内存:

working_set = "Dog eats treats"
a = "Dog" 
b = "eats"
c = "treats"

最后,a = 表示将我的指针设置为指向结果集。

内存:

a = "Dog eats treats"
b = "eats"
c = "treats"

"Dog" 被回收,因为不再有指针连接到它的内存块。我们从未修改过“Dog”所在的内存部分,这就是不可变的含义。但是,我们可以更改哪些标签(如果有)指向该内存部分。

The statement a = a + " " + b + " " + c can be broken down based upon pointers.

a + " " says give me what a points to, which can't be changed, and add " " to my current working set.

memory:

working_set = "Dog "
a = "Dog" 
b = "eats"
c = "treats"

+ b says give me what b points to, which can't be changed, and add it to current working set.

memory:

working_set = "Dog eats"
a = "Dog" 
b = "eats"
c = "treats"

+ " " + c says add " " to the current set. Then give me what c points to, which can't be changed, and add it to current working set.
memory:

working_set = "Dog eats treats"
a = "Dog" 
b = "eats"
c = "treats"

Finally, a = says set my pointer to point to the resulting set.

memory:

a = "Dog eats treats"
b = "eats"
c = "treats"

"Dog" is reclaimed, because no more pointers connect to it's chunk of memory. We never modified the memory section "Dog" resided in, which is what is meant by immutable. However, we can change which labels, if any, point to that section of memory.

缘字诀 2025-01-08 00:59:22

数据和与其关联的标签之间存在差异。例如,当您执行此操作时,

a = "dog"

将创建数据“dog”并将其放置在标签a下。标签可以改变,但内存中的内容不会改变。在您执行此操作后,数据“dog”仍将存在于内存中(直到垃圾收集器将其删除)。

a = "cat"

在您的程序a中,现在^指向^“cat” 但字符串 "dog" 没有改变。

There is a difference between data and the label it is associated with. For example when you do

a = "dog"

the data "dog" is created and put under the label a. The label can change but what is in the memory won't. The data "dog" will still exist in memory (until the garbage collector deletes it) after you do

a = "cat"

In your programm a now ^points to^ "cat" but the string "dog" hasn't changed.

悲欢浪云 2025-01-08 00:59:22
l = [1,2,3]
print id(l)
l.append(4)
print id(l) #object l is the same

a = "dog"
print id(a)
a = "cat"
print id(a) #object a is a new object, previous one is deleted
l = [1,2,3]
print id(l)
l.append(4)
print id(l) #object l is the same

a = "dog"
print id(a)
a = "cat"
print id(a) #object a is a new object, previous one is deleted
尘曦 2025-01-08 00:59:22

Python 字符串对象是不可变的。
示例:

>>> a = 'tanim'
>>> 'Address of a is:{}'.format(id(a))
'Address of a is:64281536'
>>> a = 'ahmed'
>>> 'Address of a is:{}'.format(id(a))
'Address of a is:64281600'

在此示例中,我们可以看到,当我们在 a 中分配不同的值时,它不会修改。创建一个新对象。
并且无法修改。
示例:

  >>> a[0] = 'c'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    **TypeError**: 'str' object does not support item assignment

发生错误。

Python string objects are immutable.
Example:

>>> a = 'tanim'
>>> 'Address of a is:{}'.format(id(a))
'Address of a is:64281536'
>>> a = 'ahmed'
>>> 'Address of a is:{}'.format(id(a))
'Address of a is:64281600'

In this example we can see that when we assign different value in a it doesn't modify.A new object is created.
And it can't be modified.
Example:

  >>> a[0] = 'c'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    **TypeError**: 'str' object does not support item assignment

A error occurs.

冷…雨湿花 2025-01-08 00:59:22

Python 字符串是不可变的。但是,a 不是字符串:它是具有字符串值的变量。您无法更改字符串,但可以将变量的值更改为新字符串。

Python strings are immutable. However, a is not a string: it is a variable with a string value. You can't mutate the string, but can change what value of the variable to a new string.

[浮城] 2025-01-08 00:59:22

变量可以指向任何他们想要的地方。
如果执行以下操作,将会引发错误:

a = "dog"
print a                   #dog
a[1] = "g"                #ERROR!!!!!! STRINGS ARE IMMUTABLE

Variables can point to anywhere they want..
An error will be thrown if you do the following:

a = "dog"
print a                   #dog
a[1] = "g"                #ERROR!!!!!! STRINGS ARE IMMUTABLE
长伴 2025-01-08 00:59:22

'mutable' 意味着我们可以改变字符串的内容,
“不可变”意味着我们不能添加额外的字符串。

点击查看照片证明

'mutable' means that we can change the content of the string,
'immutable' means that we can't add an extra string.

click for photo proof

|煩躁 2025-01-08 00:59:22
a = 'dog'
address = id(a)
print(id(a))

a = a + 'cat'
print(id(a))      #Address changes

import ctypes
ctypes.cast(address, ctypes.py_object).value    #value at old address is intact
a = 'dog'
address = id(a)
print(id(a))

a = a + 'cat'
print(id(a))      #Address changes

import ctypes
ctypes.cast(address, ctypes.py_object).value    #value at old address is intact
绝不放开 2025-01-08 00:59:22

<代码>>> a = '狗'

>>>>> a.replace('狗', '狗吃零食')

'狗吃零食'

>>> print a

'dogs'

不可变的,不是吗?!

变量改变部分已经讨论过了。

>>> a = 'dogs'

>>> a.replace('dogs', 'dogs eat treats')

'dogs eat treats'

>>> print a

'dogs'

Immutable, isn't it?!

The variable change part has already been discussed.

趴在窗边数星星i 2025-01-08 00:59:22

考虑在您的示例中添加此内容

 a = "Dog"
 b = "eats"
 c = "treats"
 print (a,b,c)
 #Dog eats treats
 d = a + " " + b + " " + c
 print (a)
 #Dog
 print (d)
 #Dog eats treats

我在博客中找到的更准确的解释之一是:

在Python中,(几乎)一切都是对象。我们在 Python 中通常所说的“变量”更恰当地称为名称。同样,“赋值”实际上是将名称绑定到对象。每个绑定都有一个定义其可见性的范围,通常是名称起源的块。

例如:

some_guy = 'Fred'
# ...
some_guy = 'George'

当我们稍后说 some_guy = 'George' 时,包含 'Fred' 的字符串对象不受影响。我们刚刚更改了名称 some_guy 的绑定。但是,我们还没有更改“Fred”或“George”字符串对象。就我们而言,它们可能会无限期地存在。

博客链接: https://jeffknupp .com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/

Consider this addition to your example

 a = "Dog"
 b = "eats"
 c = "treats"
 print (a,b,c)
 #Dog eats treats
 d = a + " " + b + " " + c
 print (a)
 #Dog
 print (d)
 #Dog eats treats

One of the more precise explanations I found in a blog is:

In Python, (almost) everything is an object. What we commonly refer to as "variables" in Python are more properly called names. Likewise, "assignment" is really the binding of a name to an object. Each binding has a scope that defines its visibility, usually the block in which the name originates.

Eg:

some_guy = 'Fred'
# ...
some_guy = 'George'

When we later say some_guy = 'George', the string object containing 'Fred' is unaffected. We've just changed the binding of the name some_guy. We haven't, however, changed either the 'Fred' or 'George' string objects. As far as we're concerned, they may live on indefinitely.

Link to blog: https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/

作业与我同在 2025-01-08 00:59:22

在上述答案的基础上再补充一点。

变量的id在重新分配时会发生变化。

>>> a = 'initial_string'
>>> id(a)
139982120425648
>>> a = 'new_string'
>>> id(a)
139982120425776

这意味着我们已经改变了变量a以指向一个新字符串。现在存在两个 string(str) 对象:

'initial_string'id = 139982120425648

' new_string' with id = 139982120425776

考虑下面的代码:

>>> b = 'intitial_string'
>>> id(b)
139982120425648

现在,b 指向'initial_string' 与重新分配之前的 a 具有相同的 id

因此,'intial_string' 尚未发生突变。

Adding a bit more to above-mentioned answers.

id of a variable changes upon reassignment.

>>> a = 'initial_string'
>>> id(a)
139982120425648
>>> a = 'new_string'
>>> id(a)
139982120425776

Which means that we have mutated the variable a to point to a new string. Now there exist two string(str) objects:

'initial_string' with id = 139982120425648

and

'new_string' with id = 139982120425776

Consider the below code:

>>> b = 'intitial_string'
>>> id(b)
139982120425648

Now, b points to the 'initial_string' and has the same id as a had before reassignment.

Thus, the 'intial_string' has not been mutated.

腻橙味 2025-01-08 00:59:22

内置函数id()以整数形式返回对象的标识。该整数通常对应于对象在内存中的位置。

\>>a='dog'
\>>print(id(a))

139831803293008

\>>a=a+'cat'
\>>print(id(a))

139831803293120

最初,“a”存储在 139831803293008 内存位置,因为字符串对象在 python 中是不可变的,如果您尝试修改和重新分配,引用将被删除,并将成为指向新内存位置的指针(139831803293120)。

The built-in function id() returns the identity of an object as an integer. This integer usually corresponds to the object’s location in memory.

\>>a='dog'
\>>print(id(a))

139831803293008

\>>a=a+'cat'
\>>print(id(a))

139831803293120

Initially, 'a' is stored in 139831803293008 memory location, as the string object is immutable in python if you try to modify and reassign the reference will be removed and will be a pointer to a new memory location(139831803293120).

七秒鱼° 2025-01-08 00:59:22

总结:

a = 3
b = a
a = 3+2
print b
# 5

不是不可变的:

a = 'OOP'
b = a
a = 'p'+a
print b
# OOP

不可变的:

a = [1,2,3]
b = range(len(a))
for i in range(len(a)):
    b[i] = a[i]+1

这是 Python 3 中的一个错误,因为它是不可变的。在 Python 2 中这不是一个错误,因为显然它不是不可变的。

Summarizing:

a = 3
b = a
a = 3+2
print b
# 5

Not immutable:

a = 'OOP'
b = a
a = 'p'+a
print b
# OOP

Immutable:

a = [1,2,3]
b = range(len(a))
for i in range(len(a)):
    b[i] = a[i]+1

This is an error in Python 3 because it is immutable. And not an error in Python 2 because clearly it is not immutable.

拥抱影子 2025-01-08 00:59:22

我们只是连接两个字符串值。我们永远不会改变 (a) 的值。刚才(a)代表另一个具有“dogdog”值的内存块。因为在后端,一个变量永远不会同时代表两个内存块。连接前(a)的值为“dog”。但之后 (a) 代表“dogdog”,因为现在 (a) 在后端代表中。具有“dogdog”值的块。而“狗”是代表。在 (b) 代表“dog”之前,(b) 和“dog”不会被算作垃圾值。

令人困惑的是我们在后端用相同的变量名表示内存块(包含数据或信息)。

We r just concatenate the two string values. We never change the value of (a). Just now (a) represent another memory block that has "dogdog" value. Because in the backend, one variable never represent two memory blocks at same time. The value of (a) before concatenation was "dog". But after that (a) represent the "dogdog", because now (a) in backend rep. the block that has "dogdog" value. And "dog" is rep. by (b) and "dog" isn't counted as garbage value until (b) represent the "dog".

The confusion is we represent the memory blocks(that contain data or info.) in backend with same variable name.

想你的星星会说话 2025-01-08 00:59:22

您可以使 numpy 数组不可变并使用第一个元素:

numpyarrayname[0] = "write once"

then:

numpyarrayname.setflags(write=False)

numpyarrayname.flags.writeable = False

You can make a numpy array immutable and use the first element:

numpyarrayname[0] = "write once"

then:

numpyarrayname.setflags(write=False)

or

numpyarrayname.flags.writeable = False
等待圉鍢 2025-01-08 00:59:22

这张图给出了答案。请阅读它。

输入图片此处描述

This image gives the answer. Please read it.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文