References and shared references to objects in Python

Source: Internet
Author: User

First create an object in Python, and then point the variable to the object you created.

For each object, there is a header message that marks the object's type information in the message. Whenever a variable name is given a new object, the space occupied by the previous object is recycled (if the object is not referenced by another variable name or object). Also, in Python, each object has a counter that records the number of times the object is referenced, and when the counter is set to 0 o'clock, the memory space occupied by the object is freed, which is garbage collection.

Shared references:

In Python, a variable is always a pointer to an object, not a memory area label that can be changed. Give a variable a new value, not replace the original object, but let the variable to refer to a new object, the actual effect is to re-assign the value of the variable, which will only affect the re-assigned variable.

Also, here is the operator "= =" and "is".

= =, in the same way as the C language, tests the values of the two referenced objects in a consistent way;

is, checks the identity of two objects, determines whether two object names point to the same address space, and returns True if it is. ,

Create two list objects, even if their values are the same, from different spaces. So is the comparison result is false

Maybe some friends have tested an example, the result is actually return true. This does not mean that they point to the same object, but in memory, small integers and strings are cached and reused , so is only tells us that A and B refer to the same object.

References and shared references to objects in Python

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.