In Python, when we make a variable equal to another variable, we do not pass the value to it, but directly change the pointed address. If we want to check the address of a variable in memory, we can check it by id (variable). Let's take a look at this interesting process through a small example.
>>> x = 12 >>> y= 13 >>> id(x)1865402384 >>> id(y)1865402416 >>> x = y>>> id(x)1865402416 >>> id(y)1865402416
First assign the value of 12 to the x variable and 13 to the y variable. We use id (variable) to view the locations of x and y in memory respectively. The above display is 1865402384 and 1865402416 respectively. Then after we set x = y, we checked their locations in memory and found that both x and y pointed to 1865402416. It can be seen that in python, the way we assign values is different from that in C language. C language directly changes the value in the memory of x, while Python directly changes the pointer of x, which reminds me of pointers.
Let’s give it a try and continue to enter the following code here
>>> y = 12 >>> id(y)1865402384
Days! what happened? ? The address of y in the memory becomes 1865402384 again. To be precise, y points to the memory area 1865402384 again. Looking at it this way, it is really similar to the pointers in C language.
The above is the detailed content of Python's secret introduction to variable assignment. For more information, please follow other related articles on the PHP Chinese website!