On the reference of python variable and the principle of storing it at the bottom

  • 2021-12-04 10:31:17
  • OfStack

Directory 1. The underlying principle of variable reference 2. Classification of variables

Python variables, simply speaking, there are 6 categories, such as numeric type, Boolean type, string type, list, tuple and dictionary. Then how to store different variable types in the bottom layer is related to the reference of variables and whether we can correctly grasp the related operations of variables

What are the values of v1 and v2 below? Why?


v1 =3
v2=v1
print("v2:",v2)
v1 += 2
print("v1:",v1)
print("v2:",v2)
 


# What is the value of l2 below? Why?


l1 = [1,2,3]
l2 =l1
print(l2)
l1.append(4)
print(l2)


1. The underlying principle of variable reference


v1 =3
v2=v1            
print("v2:",v2)
v1 += 2          
print("v1:",v1)  
print("v2:",v2)   #v1 The value of has changed, why v2 Has not changed the value of? 
'''


The implementation results are as follows:

v1: 5
v2: 3
'''


l1 = [1,2,3]
l2 =l1
print(l2)
l1.append(4)
print(l2)    #l1 The value of has changed, but why l2 The value of has also changed. 
'''

The implementation results are as follows:

[1, 2, 3]
[1, 2, 3, 4]

'''

Key points analysis:

Variable objects and immutable objects: Immutable objects include int , float , string , tuple Wait. Variable objects include list, dict, instances of custom classes, and so on. In python The variables copied by = in are all copied at the same time, that is, the value of the variable and the address of the variable in memory. That is, = the copied variable not only has 1 memory address, but also has 1 value. However, it should be noted that for variables of immutable types (such as int), if you want to change the value of the variable, a new value will be created, and a new memory address will be assigned to this new value, and then the variable will be pointed to the memory address of this new value, while the old value will wait for garbage collection if it is not referenced. If it is a variable of variable type. If you modify the value of a variable, you can modify the value of the variable directly, and the reference address of the variable will not change.

Immutable type: variable assignment a=5 After that, a=10 is assigned. In fact, a new int value object 10 is generated, and then a points to it, while 5 is discarded, instead of changing the value of a, it is equivalent to a new a.

Variable type: variable assignment la=[1,2,3,4] After assigning la [2] = 5, the value of the third element of list la is changed, and la itself has not moved, but its internal part of the value has been modified.

The essence is that after the immutable object 1 is newly built, the system will allocate fixed memory to him according to his size, so it is not allowed to modify it. Only modifying the value can only apply for new memory and address. For variable objects, their memory size can be automatically expanded with the change of value

Code analysis demo:

Because when a variable is created in memory, the system will assign it an address, and then find or refer to its value through the address. All variables in Python are actually 1 pointer to an object in memory, which is a reference to a value

Code demo 1:


v1 =3
v2=v1
print("v1 Address in memory: %d,v2 In-memory address :%d"%(id(v1),id(v2)))
v1 += 2
print("v1 Address in memory: %d,v2 In-memory address :%d"%(id(v1),id(v2)))
print("v1:",v1)
print("v2:",v2)
========================================================
v1 Address in memory: 1747378992,v2 In-memory address :1747378992
v1 Address in memory: 1747379024,v2 In-memory address :1747378992
v1: 5
v2: 3
'''


Because v1 and v2 are both int, which are immutable objects, the system will recreate one for them in memory once their values are modified, and operations such as addition, subtraction, multiplication and division are deducted.
This variable is then bound (by address reference) to this value, so after v1 + = 2, its address in memory changes. v2 still refers to the previous address, so the value of v2
There is no update.
'''

Code demo 2:


l1 = [1,2,3]
l2 =l1
print("l1 Address in memory: %d,l2 In-memory address :%d"%(id(l1),id(l2)))
print(l2)
print("l1 Address in memory: %d,l2 In-memory address :%d"%(id(l1),id(l2)))
l1.append(4)
print(l2)
print("l1 Address in memory: %d"%(id(l1)))
=======================================================
l1 Address in memory: 37345880,l2 In-memory address :37345880
[1, 2, 3]
l1 Address in memory: 37345880,l2 In-memory address :37345880
[1, 2, 3, 4]
l1 Address in memory: 37345880

For mutable objects such as list, l1, l2, when modifying their values, the system will automatically expand their memory on the original address, so there is no need to modify the address.

Code demo 3: Similarly, the reference effect of immutable variables such as strings is also 1.


str1 ='hahhahah'
str2 = str1
str1 += "123"
print(str1,str2)
hahhahah123 hahhahah

2. Classification of variables

Variable types and immutable types

Variable type, the value can be changed:

List list Dictionary dict

Immutable type, the value cannot be changed:

Numeric type int , float0 , bool , float String str Tuple tuple

Related articles: