<python variable and immutable data types >Python is not the same as C + +, its variable use has its own characteristics, when learning python, it is important to remember that "everything is the object, everything is a reference to the object," the sentence, in fact, this feature is similar to Java, so in Python you do not have to worry about similar to C + + A complex problem with pointers in thein Python, data is divided into
Variable data Types,
Immutable data Types. ●variable data types: List and dictionary dict. Immutable data types: int, float, string, and tuple tuple. take int and list as an example to analyze the difference between a "mutable data type" and an "immutable data type." ? Immutable data type Analysis
>>> x = 1 >>> id(x) 31106520 >>> y = 1 >>> id(y) 31106520 >>> x = 2 >>> id(x) 31106508 >>> y = 2 >>> id(y) 31106508 >>> z = y >>> id(z) 31106508
>>> x += 2 >>> id(x) 31106484
The above procedure is an operation of type int in the immutable data type, and the
ID () looks at the address value of the current variable . Let's take a look at the result of x = 1 and y = 12 operations, from the above output you can see that
x and Y address values are the same at this point , that
is, X and y actually refer to the same object , that is,
1, which means In memory, only one address is used for 1 , and no matter how many references point to it , there is only one address value , but there is only a reference count that records a reference to that address. When we make the X = 2 assignment, we find that the address value of x is changed, although it is the reference to X, but its address value is changed, the following y = 2 and z = y, so that x, Y and Z all refer to the same object, that is, 2, so the address value is the same. When x and Y are assigned a value of 2, 1 This object has no reference to it, so 1 of the object occupied by the memory, that is, 31106520 address to be "garbage collected", that is, 1 of the object in memory no longer exists. Finally, X has a 2 operation, so a new object is created 4,x references the new object and no longer references the 2 object. is called an immutable data type,the
immutable here can be understood as the value at the address referenced by x cannot be changed , that is, the value at the 31106520 address is always 1 before garbage collection, cannot be changed, if you want to assign X to 2, Then only the X-referenced address from 31106520 to 31106508, the equivalent of x = 2 This assignment creates an object, the object 2, and then X, Y, Z all refer to this object, so the data type int is immutable, if you want to assign a value to a variable of type int, The equivalent of creating a new object in memory is no longer the previous object.
Figure 1 python immutable data type analysisAs you can see from the above procedure, the advantage of an immutable data type is that no matter how many references are in memory, the same object occupies only one piece of memory, but its
disadvantage is that when you need
to operate on the variable to change the value of the object referenced by the variable , because it is an immutable data type, a
new object must be created so that a new object is created once and for all, but the memory that is no longer used is reclaimed by the garbage collector. ? variable data type analysis
>>> a = [1, 2, 3] >>> id(a) 41568816 >>> a = [1, 2, 3] >>> id(a) 41575088 >>> a.append(4) >>> id(a) 41575088 >>> a += [2] >>> id(a) 41575088 >>> a [1, 2, 3, 4, 2]
As can be seen from the above program,
two times a = [1, 2, 3] operation, two times a reference address value is different , that is, actually created two different objects, which is obviously different from the immutable data type, so
For a mutable data type, an object with the same value is a different object, that is, an object that holds multiple identical values in memory, with different address values. Next, we add operations to the list, respectively A.append (4) and A + = [2], and
found that these two operations make a reference to the value of the object as the final result, but a reference to the address is still 41575088, that is, a Operation does not change the address value of a reference , but only after the address of the extension of the
new address , changing the value stored in the address, so the variable data type means that the operation of a variable, its value is variable, the value changes will not cause the new object, That is, the address will not change, but the content of the address has changed or the address has been expanded.
Figure 2 python variable data type analysis As you can see from the above procedure, a mutable data type is one that allows the content of the same object, that is, the value can vary, but the address does not change. However, it is important to note that the operation of a mutable data type cannot be a direct new assignment operation, such as a = [1, 2, 3, 4, 5, 6, 7], so that the operation is not a change of value, but a new object is created, where the variable is only similar to append, + = and other such operations. ? SummaryThe above process is summed up in one sentence:
"The immutable data type in Python, does not allow the value of the variable to change, if the value of the variable is changed, the equivalent of a new object, and for the same value of the object, in memory there is only an object, Internally there will be a reference count to record how many variables refer to the object , and the
mutable data type , which allows the variable's value to change, that is, if the variable is append, + =, and so on, it only changes the value of the variable, and does not create a new object. The address of the object referenced by the variable does not change, but for different objects of the same value, there are different objects in memory, that is, each object has its own address, which is equivalent to storing multiple copies of the object in memory for the same value, there is no reference count, and it is a real object. "
<wiz_tmp_tag id= "Wiz-table-range-border" contenteditable= "false" style= "Display:none;" >
From for notes (Wiz)
Variable and immutable data types for Python