The following editor will bring you a brief discussion of mutable objects and immutable objects in Python. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
What is a mutable/immutable object
Immutable object, the memory pointed to by the object The value in cannot be changed. When a variable is changed, since the value it refers to cannot be changed, it is equivalent to copying the original value and then changing it. This will open up a new address, and the variable will point to this new address. address.
Variable object, the value in the memory pointed to by the object can be changed. After the variable (quote to be precise) is changed, the value it refers to is actually changed directly. There is no copying behavior and no new outgoing address is opened. In layman's terms, it is changed in place. .
In Python, numeric types (int and float), stringsstr, and tuple tuples are all immutable types. Lists, dictionaries, and sets are variable types.
It is more intuitive to look at the code. Let’s first look at the immutable objects
Examples of immutable objects
Let’s first explain that is is to determine whether the ids of two objects are the same, and = = What is judged is whether the contents are the same.
a = 2 b = 2 c = a + 0 c += 0 print(id(a), id(b), id(2)) # id都相同 print(c is b) #True
Look at the string
astr = 'good' bstr = 'good' cstr = astr + '' print(cstr is bstr) # True print(id(astr), id(bstr), id('good')) # 三个id相同
The result is the same as the numerical type. If the following situation occurs, the variable is no longer good
astr = 'good' print(id(astr)) astr += 'aa' print(id(astr)) # id和上面的不一样
after modification. Since it is an immutable object, the value of the memory corresponding to the variable is not allowed to be changed. When a variable is to be changed, the original value is actually copied and then changed, a new address is opened, and astr points to this new address (so the id of the previous and the previous astr is different). The value corresponding to the original astr is different because If another object points to it, it will be garbage collected. This is the same for int and float types.
Look at the tuple again
##
add = (1, 2, 3) aee = (1, 2, 3) print(id(add), id(aee), id((1, 2, 3))) # id各不相同 aee = (1, 2, 3) print(id(aee)) aee += () # 加空元组 print(id(aee)) # id变了! print(aee) #(1 ,2,3)
add = (1, 2, 3) aee = add print(id(aee), id(add)) # 这两个id一样 aee += (4, 5, 6) print(id(aee)) # aee的id变了! print(add) # add还是(1, 2, 3)没有变
a = 2.0 b = 2 print(a is b) # False, 一个int一个float,类型都不同
Examples of variable objects
lis = [1, 2, 3] lis2 = [1, 2, 3] # 虽然它们的内容一样,但是它们指向的是不同的内存地址 print(lis is lis2) print(id(lis), id(lis2), id([1, 2, 3])) # 三个id都不同
alist = [1, 2, 3] # alist实际上是对对象的引用,blist = alist即引用的传递,现在两个引用都指向了同一个对象(地址) blist = alist print(id(alist), id(blist)) # id一样 # 所以其中一个变化,会影响到另外一个 blist.append(4) print(alist) # 改变blist, alist也变成了[1 ,2 ,3 4] print(id(alist), id(blist)) # id一样,和上面值没有改变时候的id也一样
abb = {1, 2, 3} acc = abb print(id(abb), id(acc)) acc.add(4) print(abb) # {1, 2, 3, 4} print(id(abb), id(acc)) # 相等
abc = 3 dd = abc dd = 43 print(abc) # 3,并不随dd的改变而改变
blist = alist[:] # or alist.copy() print(alist is blist) # False blist.append(4) print(alist) # 还是[1,2 ,3]没有变化
test_list = [1, 2, 3, 4] test_str = 'HAHA' def change(alist): alist.append(5) def not_change(astr): astr.lower() change(test_list) not_change(test_str) print(test_list) # 改变了原来的值 print(test_str) # 没有变
Interesting example
Looking at another interesting example, we know that list can be added to a list using +.a1 = [1, 2, 3] a2 = a1 print(id(a1), id(a2)) # 实际上是a2指向了新的对象,id已经改变。 # 所以现在a2、a1并不是同一对象的两个引用了,a2变化a1不会改变 a2 = a2 + [4] # 这个等式中,右边的a2还是和a1的id一样的,一旦赋值成功,a2就指向新的对象 print(id(1), id(a2)) # 不等,a2的id变化了 print(a1) # [1, 2, 3]没有变
a1 = [1, 2, 3] a2 = a1 print(id(a1), id(a2)) a2 += [4] # 相当于调用了a2.extend([4]),原地改变并没有新的对象产生 print(id(1), id(a2)) # 相等,a2的id没有变化 print(a1)
The above is the detailed content of An explanation of mutable objects and immutable objects in Python. For more information, please follow other related articles on the PHP Chinese website!