What is a mutable/immutable object
- An immutable object in which the object points to an in-memory value that cannot be changed. when a variable is changed, because the value it refers to cannot be changed, it is equivalent to copy the original value and then change, this will open a new address, the variable point to the new address.
- A mutable object in which the object points to an in-memory value that can be changed. variables (accurately referred to) after the change, is actually the value of its direct change, and did not occur replication behavior, nor open up new addresses, popular point is to change in situ .
In Python, numeric types ( int and float ), strings str, tuple tuples are immutable types. The list, dictionary dict, and set set are mutable types.
Or see the code more intuitive. See Non-mutable objects first
Examples of immutable objects
The first point is is to determine whether the IDs of two objects are the same, and == whether the content is the same.
= 2b = 2c = a + 0 c += 0print(id(a), id(b), id(2)) # id都相同print(c is b) #True
Look again at the string
= ‘good‘bstr = ‘good‘cstr = astr + ‘‘print(cstr is bstr) # Trueprint(id(astr), id(bstr), id(‘good‘)) # 三个id相同
Is the same as the result of a numeric type. If this is the case, the variable is not modified after thegood
= ‘good‘print(id(astr))astr += ‘aa‘print(id(astr)) # id和上面的不一样
because it is an immutable object, the value of the variable corresponding to the memory is not allowed to be changed. When the variable to change, in fact, the original value is copied a copy and then change, open a new address, astr again point to the new address (so the ID of the front and back astr is not the same), the original astr corresponding value because no longer have the object point to it, it will be garbage collection. 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, < Span class= "Hljs-number" >3))) # ID varies aee = (1, 2, 3) print (id (AEE)) Aee += () # add empty tuple print (id (AEE)) # ID changed! print (AEE) # (1, 2,3)
Although it appears to be (1, 2, 3) it should be consistent with the above. Is this a mutable object? Look at
again.
add = (< Span class= "Hljs-number" >1, 2, 3) aee = add print (id (AEE), id (add) # these two IDs aee += ( 4, 5, 6) Print (id (AEE)) # AEE ID changed! print (add) # Add or (1, 2, 3) not changed
And the value type is the same as the STR type. If it is a mutable object add = aee , they point to the same address (the same ID) is positive. But not a different reference to the same object, because if so, the change of AEE will cause the change of add, which is not the case in the tuple. So a tuple is an immutable object, but a little bit different from STR and numeric types. The commonly said tuple can not be changed many times refers to the stored value can not be changed ( some special cases, such as a tuple inside the list, can change the elements in the list.) But in fact the tuple has not been changed ).
For str、int、float as long as they are of the same type, the values are the same, and their IDs are the same. (Why would you say the same type?) )
= 2.0b = 2print(a is b) # False, 一个int一个float,类型都不同
2 and 2.0 are not on one address.
Examples of mutable objects
Lis= [1, 2, Span class= "DV" >3]lis2 = [1 , 2, 3]< Span class= "Hljs-comment" ># although their contents are the same, they point to different memory addresses print (lis is lis2) print (id (LIS), id (lis2), id ([1, 2 , 3])) # three IDs are different
And then look at the assignment.
Alist= [1, 2, Span class= "DV" >3]# alist is actually a reference to an object, blist = Alist refers to the pass, and now two references point to the same object (address) blist = alistprint (id (alist), span class= "bu" >id (blist)) # ID # so one of the changes will affect another blist.append (4) print (alist) # change blist, Alist also became [1, 2, 3 4]print (id (alist), id (blist)) # ID, same as when the face value has not changed.
blist = alistThis sentence. alist is actually a reference to the object, blist = alist which is the passing of the reference, and now two references point to the same object (address). So one of the changes will affect the other one.
And look at set.
= {1, 2, 3}acc = abbprint(id(abb), id(acc))acc.add(4)print(abb) # {1, 2, 3, 4} print(id(abb), id(acc)) # 相等
It is consistent with the example above list.
Mutable objects because the object can be modified, so there is no need to duplicate a copy and then change, directly in situ changes, so will not open up new memory, change the ID and the same.
Of course the immutable object is not like this, can compare with this
= 3dd = abcdd = 43print(abc) # 3,并不随dd的改变而改变
But if it is a copy, it is simply copying the contents of the past, passing it and not referencing it. This is especially useful when you want to use the values of the list and don't want to modify the original list.
= alist[:] # or alist.copy()print(alist is blist) # Falseblist.append(4)print(alist) # 还是[1,2 ,3]没有变化
As a function parameter
As a function parameter, it is the same, the mutable type passes a reference, and the immutable type passes the content.
Test_list= [1,2,3, 4]test_str = ' HAHA ' def change (alist): Alist.append (5) < Span class= "hljs-function" >def Not_change (ASTR): Astr.lower () Change (test_list) not_change (test_str) # changed the original value print (test_str) # not changed
Of course, if you don't want to change the value of the original list, the parameter can pass in the column's copy.alsit[:]
An interesting example
Looking at an interesting example, we know that list can be used to + add a listing.
A1= [1, 2, 3]a2 = a1print (id (A1), id (A2)) # is actually A2 pointing to the new object, the ID has changed. # so now A2, A1 is not two references to the same object, A2 change A1 does not change A2 = A2 + [4] # In this equation, the A2 on the right is the same as the A1 ID, and once the assignment succeeds, A2 points to the new object print (id (1), id (A2)) #, A2 ID changed print (A1) # [1, 2, 3] not changed
If this is the case
= [1, 2, 3]a2 = a1print(id(a1), id(a2))a2 += [4] # 相当于调用了a2.extend([4]),原地改变并没有新的对象产生print(id(1), id(a2)) # 相等,a2的id没有变化print(a1)
The difference is that a2 += [4] this is equivalent to invoking a a2.extend([4]) change in place, and no new object is generated.
Mutable and immutable objects in Python