Aren't Python strings immutable? Then why does

2019-01-03 02:11发布

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?

18条回答
Lonely孤独者°
2楼-- · 2019-01-03 02:37

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.

查看更多
再贱就再见
3楼-- · 2019-01-03 02:37

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.

查看更多
\"骚年 ilove
4楼-- · 2019-01-03 02:37

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/

查看更多
Melony?
5楼-- · 2019-01-03 02:41

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.

查看更多
女痞
6楼-- · 2019-01-03 02:42
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
查看更多
我命由我不由天
7楼-- · 2019-01-03 02:42

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.

查看更多
登录 后发表回答