Finally, it is important to understand the representation of variables in computer memory. When we write:
a = ‘ABC‘
, the Python interpreter did two things:
A string is created in memory ‘ABC‘
;
A variable named in memory is created a
and pointed to ‘ABC‘
.
You can also assign a variable a
to another variable, which actually refers to the data that the variable points to b
b
a
, such as the following code:
‘ABC‘b = aa = ‘XYZ‘print b
The last line prints out b
the contents of the variable. ‘ABC‘
‘XYZ‘
If it is mathematically understood, it will be wrong to draw the b
same, and it a
should be ‘XYZ‘
, but b
the actual value is ‘ABC‘
, let us execute the code in one line, we can see exactly what happened:
Execution a = ‘ABC‘
, the interpreter creates a string ‘ABC‘
and a variable a
, and a
points to ‘ABC‘
:
Executes b = a
, the interpreter creates the variable b
and points to b
a
the string that points to ‘ABC‘
:
Execute a = ‘XYZ‘
, the interpreter creates the string ' XYZ ' and puts a
the pointer instead ‘XYZ‘
, but b
does not change:
So, the b
result of the last print variable is naturally ‘ABC‘
.
python--the representation of computer memory in a variable