Python beginner ____ Explore _____ (1) Variable Object immutable object understanding

Source: Internet
Author: User
Tags garbage collection

1. Variable Objects & immutable objects

① for Python, the meaning of the variable = object means that the variable is bound to the object as a reference, and the object can be used with the variable name when the binding is complete. when an object does not have any object references, it enters the garbage collection process ( Automatic recycling mechanism).

② for a multi-element data structure, such as list,tuple,dict, its implementation is bound to the target object by an array of pointers.

1.1 Variable objects

mutable objects in Python tend to be multi-element data structures, most of which are bound to element objects through pointer arrays , mutable in multi-element mutable objects, meaning that the array of pointers is mutable (pointer arrays can be scaled down and array elements can be modified).

Example 1:

LT1 = ["sy","wsx","syf",  ]print"wsx"lt1.append Print (ID (LT1)) Print (ID (lt1[1])) Print (LT1)

In Example 1, we modify lt1[0]= "WSX", that is, to modify the pointer array in the NO. 0 pointer to bind to the value of "WSX" on the object, because its previously bound "sy" string belongs to a non-mutable object, so it must discard and create a "wsx", because previously in memory has a " Wsx ", so we let him bind to this. So you can see that the ID (LT) and the ID (lt[1]) are the same for many results. Similarly, we let LT1 add an element, because it is a mutable object, insert an element directly at the end of the array to point to the object 100.

In the above procedure, the array pointers for these multi-element mutable objects have changed .

1.2 Immutable objects

There are two basic types of Python immutable objects that can be learned: one is implemented by a pointer array (such as a tuple), and the other is not implemented by a pointer array .

(1) For tuple these immutable objects, which are implemented by pointer arrays, are immutable mainly because their pointer arrays are immutable .

Example 1:

Tup1 = ("syf","wsx","sy", [100 ]) tup1[3][0]=99tup1[3].append (0)print(tup1)

You can see that Tup1 is an immutable object, but eventually its tup[3] element is still modified, but in the process we did not modify the pointer element in the TUP1 pointer array, tup[3] still points to the original list.

(2) Another kind is like str,int such as immutable, its immutable main embodiment in the content is immutable. For immutable objects in the form of non-array pointers, they will be passed directly to create a new target object if we modify it.

i =  2 + + +

Python beginner ____ Explore _____ (1) Variable Object immutable object understanding

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.