Transferred from: http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001431658624177ea4f8fcb06bc4d0e8aab2fd7aa65dd95000
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:
A = ' 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‘
.
The representation of a Python variable in computer memory