python--the representation of computer memory in a variable

Source: Internet
Author: User

Finally, it is important to understand the representation of variables in computer memory. 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:

‘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

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.