The representation of a Python variable in computer memory

Source: Internet
Author: User

Transferred from: http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001431658624177ea4f8fcb06bc4d0e8aab2fd7aa65dd95000

When we write:

a = ‘ABC‘

, the Python interpreter did two things:

    1. A string is created in memory ‘ABC‘ ;

    2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.