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, in fact, it refers to the value of the direct change, and there is no replication behavior, there is no new address, the popular point is to change in situ .
In Python, Int,float,string,tuple,bytes is an immutable object, and Set,list,dict is a mutable object.
Int:
1 >>> a =2 >>> ID (a)3 1402899362180484 >>> A = A + 15 >>> ID (a)6 140289936218024
View Code
Float
1 >>> a = 10.552 >>> ID (a)3 1402899362260244 >>> A = a + 1.55 >>> ID (a)6 140289936226000
View Code
String
1 ' ABC ' 2 >>> ID (a)3 43561351124'd'5 >>> ID (a)6 4357016240
View Code
Tuple:
1 >>> a = (1, 2, 3)2 >>> ID (a)3 43569135844 > >> A = (4, 5, 6)5 >>> ID (a)6 4356799536
View Code
bytes
1 >>> a = b'abc'2 >>> ID (a)3 43561351124'def'5 >>> ID (a)6 4357016288
View Code
Set
1 >>> a = set ([1, 2, 3])2 >>> ID (a)3 43567542244 >>> A.add (4)5 >>> ID (a)6 4356754224
View Code
List
1 >>> a = [1, 2, 3]2 >>> ID (a)3 43569056564 > >> A.append (4)5 >>> ID (a)6 4356905656
View Code
Dict
1 >>> a = {'one'and ' both ': 2}2 >>> ID (a)3 43570245764 >>> a['three '] = 35 >>> ID (a)6 4357024576
View Code
Python mutable objects and immutable objects