Variability and address
Quite important
Last updated
Quite important
Last updated
Python is a label language, which is very different from the C language.
In python, each variable name is a pointer to the corresponding memory unit.
We can use the id() method to get the address of the memory unit pointed to by a variable.
In this example, the variable name a points to the memory address numbered 140735234512784, and the memory address stores an int type data, the value of this data is 5.
As shown in the figure below:
If I copy a variable in python, for example, as shown below:
You will find that the memory address units pointed to by a and b are the same! That is to say, when I tried to copy a variable, I didn't really clone the variable, but: I created another pointer points to the memory, and the two pointers point to the same memory space.
This means that when I try to change the content of a, the content of b will also change???
Not always, This is the real trouble!
If I change the value of a, but the address of a does not change, the value of b will also change accordingly.
Then we say that this is a variable data type.
We need to pay extra attention to this pattern!
But if I change the value of a, but a points to another address, then b still points to the original address, and the values of a and b are different.
Then we say that this is an immutable data type.
Numbers are immutable
Number is an immutable data type. When I assign another number to a variable, the address of the variable changes.
Strings are immutable
The string itself cannot be modified, So when we modify the string, python will report an error
So when the string is updated, The original string is deleted, the pointer must point to another memory location, the previous address must be changed.
String is an immutable data type. When I assign another string to a variable, the address of the variable changes as the old string is deleted.
Lists are Variable
The list is changeable, and every time the list is modified, the variable pointer of the list will not change, so when the list is copied, it cannot be simply done by assignment.
In python, we call this method of assignment shallow copy, which means that we just copy a label pointer, and the memory still shares a location with the previous variable.
We call a full copy of the memory a deep copy. In a deep copy, the data in the memory is completely copied. Of course, this will also consume more memory.
There are many ways to deep copy, for example, we can write a loop.
You may find it a bit troublesome to do so, so we can complete the deep copy through list construction.
b = [i for i in a] This is a very simple deep copy scheme. No need to introduce any libraries.
Of course, the problem is that we sometimes use high-dimensional data. If we still need to nest multiple loops in this way, it would be a little troublesome.
So we can use python library to complete deep copy.
b = copy.deepcopy(a)
This is a deep copy done using a copy library. If your data is high-dimensional data, then this is a good choice (this library python is already installed by default, and you don’t need to install it yourself)
Tuples are immutable
Tuples are unchangeable variables, so this problem does not exist with tuples.
Sets are Variable
Tuples are changeable, so a deep copy must also be performed.
Dictionary are Variable
Dictionary is changeable, so a deep copy must also be performed.
Start time of this page: December 19, 2021
Completion time of this page: December 19, 2021