Mutable objects and immutable objects in the "Go" python

Source: Internet
Author: User

Python allocates objects in the heap into two categories: mutable objects and immutable objects. The so-called mutable object means that the object's contents are mutable, such as list. Immutable objects, in contrast, indicate that their contents are immutable.

Immutable (immutable): int, string, float, (numeric number), tuple (tuple)

Variable (mutable): Word Typical (dictionary), list type, set

I. Immutable objects

Because variables in Python hold object references , object references to variables are mutable for non-mutable objects, although the object itself is immutable. The use of such mechanisms can sometimes confuse people, and it seems that mutable objects change. As in the following code:

i = 2+ + +

From the above, the immutable object has not changed, it is still an immutable object, the change is just the creation of a new object, changing the object reference of the variable .

 is ytrue  is ztrue

As shown above, because integers are immutable, x,y, Z point to a memory address that has a value of 1 in memory , that is, X, Y, Z are all pointing to the same address, and it is worth noting that, for shaping, it currently supports only (-1,100).

Summarize the pros and cons of immutable objects.

The advantage is that this reduces the amount of memory space used by duplicate values .

Disadvantage, as shown in Example 1, I want to modify the value of this variable binding, if there is no memory block of this value in memory, then you must re-open a piece of memory , the new address and the variable name binding. Instead of modifying the value of the memory block that the variable originally pointed to, this gives a certain reduction in execution efficiency .

A third example

 a= " abc    d  ={a:100 " print   (d) a  =a.replace ( a   ", "  a    )   ( A)  print  (D[ " ABC   " ])    (d)  print  (D[a]) 

Execution results are

D={a:100} After this sentence is executed, the dict is a point object, that is, ' abc ', not the A variable itself.
The A=a.replace (' A ', ' a ') then points the A variable to the new object (' ABC ')
At this point print (a) is obviously the output of ' abc ', and because the key stored in the dict is still ' abc ', the Search with ' ABC ' as key can still get the result, but if the object referred to by the A variable is used at this time (' ABC ' ) to query the dict will be an error.
Reference Address: http://blog.csdn.net/taohuaxinmu123/article/details/39008281

Mutable objects and immutable objects in the "Go" 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.