Detailed Explanation of Variable Variable and Invariable Variable in Python

  • 2021-12-05 06:43:18
  • OfStack

Directory 1, Common Variable Classification 1, Variable Creation 2. Variable Classification 1, Common Immutable Variable 2, Common Variable 3. Copy Difference 4. Parameter Transfer Difference

Foreword:

C + + is different from Python Pointers and references make it clearer when we call parameters. But Python There is no concept of pointers and references in, which leads to many questions when parameters are passed and called: Did I copy a new operation or did I operate in the memory it points to?

This problem is fundamentally related to variable and immutable variables. I would like to summarize the differences and connections between the two in order to understand them more deeply Python Internal operations. I am not a trained person, so please criticize and correct me if there are any improprieties.

1. Classification of common variables

1. Creation of variables

Before we understand the basic definitions of variable and immutable variables, we need to understand how variables are created.


x = 1


python The interpreter will determine whether 1 exists in memory. If it does not exist, python It allocates memory, creates the number 1 in memory, then sees if the variable x exists, if it does not exist, creates x, and finally assigns 1 to x.


y = 1


When you continue to enter the uplink code, 1 has already been created in memory. Do you need to recreate 1 and assign it to y now? Or just assign the 1 you just created to y? The answer is the latter. We can use id or is to determine whether it is the same address in memory.


print(id(x) == id(y))
# or 
print(x is y)


But what if we change to a list to try?


a = [1,2,3]
b = [1,2,3]
print(id(a) == id(b))


The answer is contrary to the above result, that is to say, even if there is [1, 2, 3] of x in memory, python When assigning [1, 2, 3] to y, we didn't assign the one pointed by x to the past, but opened up a new space in memory and created a new [1, 2, 3]! This leads to variable and immutable variables.

Literal meaning
According to my personal understanding, variable variables ( mutable variable ) is a variable whose contents in memory can be modified, but an immutable variable ( immutable variable ) is a variable whose contents cannot be modified in memory.

It sounds abstract. We can understand the variable name as a pointer or reference, which all point to a block of space in memory. For example, in the previous section, x and y point to the space of 1 at the same time, and a points to the part of [1, 2, 3]. So how do you understand that the contents in memory can be modified? For an int type variable, I created 1, then after that * any variable assigned 1 in this scope will point to it, and there is no external way to change this in-memory content to 2 or any other value *. For the list, I can change the block [1, 2, 3] to [1, 2, 3, 4] at any time, which means that the contents in memory can be modified.

2. Variable classification

All variables can be classified by variable and immutable variables, so I will classify some commonly used variable types.

1. Common immutable variables

Integer type Floating point type Boolean type Complex number String Tuple

2. Common variable variables

List Set Dictionary Other iterators

3. The difference between copies

copy And deepcopy Both represent copies, so the difference between them lies in mutable and immutable objects. copy And deepcopy There is no difference for immutable objects, which copy the reference to the existing space in memory; But for mutable objects, copy is a copy of a reference to an existing space in memory, and deepcopy It is to open up a new space, copy the original content completely, and then return the reference of the new space.

4. Differences in parameter passing

The process of parameter passing is also a process that is prone to confusion. C++ Parameter passing in is very clear, reference is reference and formal parameter is formal parameter, but Python No, because python There is no explicit pointer, reference in.

For immutable variables, there is a new space in the function due to the change of scope after being passed into the function, so formal parameters will be generated, and the original immutable variables will be copied, and then subsequent operations will be carried out in the function.

For a variable, its contents can be modified, so it is allowed to modify itself inside the function, so what is passed into the function is a reference to the memory space, and for this parameter operation, it will also be modified in the main function accordingly.


Related articles: