Python's Secret Introduction to Variable assignment

Source: Internet
Author: User
In Python, when we make a variable equal to another variable, it does not pass the value to it, but instead directly changes the address to the point. We want to see the address of a variable in memory, which can be viewed 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>>& Gt ID (x) 1865402416>>> ID (y) 1865402416

The first assignment of the x variable to the 12,y variable is 13, and we use the ID (variable) to see where x and Y are in memory, respectively. The above shows 1865402384 and 1865402416 respectively. Then we make x = y, and then we look at where they are in memory and find that both X and Y point to 1865402416. Thus, in Python, the way we assign values differs from the C language, and C directly changes the value in X's memory, and Python directly changes the direction of X, which reminds me of pointers.

Let's try it and continue to enter the following code here

>>> y = 12>>> id (y) 1865402384

Days! What happened?? Y's address in memory becomes 1865402384, and, exactly, Y points to 1865402384 of this area of memory. In this way, the pointers to the C language are really similar.

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.