Python immutable objects

Source: Internet
Author: User

Python immutable objects have been introduced in the Python object category, and examples are listed below for detailed analysis:

Immutable Types (numbers, strings, tuples, immutable collections): Original modifications are not supported.

Reference

>>> a=345  >>> b=a  is  b  True  >>>  

In Python everything is an object, and the object is passed by reference. When assigning a value, whether the object is newly created or an existing one, the reference to the object is assigned to the variable. So here A is actually the same object as B, and A is b for true!

1 numeric type (for example, int type)

In general, trying to modify a numeric object is actually creating a new numeric object, as shown below.

# new Digital object>>> ID (53060472L)# New Digital Object-->>> y = 1000> >> ID (y)53060328L# attempts to modify a numeric object, actually generating a new numeric object >>> y = y + 1>>> y 1001>>> ID (y)53058744L

(1) But there is a special case for small integers [-5, 256]. Given that small integers can be used frequently, for performance reasons, Python uses a small integer object buffer pool to small_ints cache integers between [-5,257], and integers in that range are shared in the Python system. Small integer objects are initialized during the py startup process, and the ob_refcnt of these small integer objects will not change and will never be >0, so the VM will not be destroyed during operation, so it plays a buffering role.

(2) for other integers beyond [-5, 257), Python also provides a dedicated buffer pool (a buffer pool for universal integer objects) for use by these so-called large integers, avoiding the need to constantly allocate memory for malloc to reduce the loss of efficiency each time it is used. Manage free space through free-list.

(3) The Int object in Python is the extension of the long type value in C.

(4) When an integer object is reclaimed, the memory is not returned to the system, but the ob_type of its object is pointed to free_list for use by the newly created object.

>>> x  = -5>>> ID (x)8676152l>>> y = -5>>> id (y)8676152l> >> x = -6>>> ID (x)53060472L>>> y = -6>>> id (y)  53059080L>>> x = 200>>> ID (x)8681192l>>> y = 200>>> id (y) 8681192L

2 String type

For example, for non-mutable object str, do the following:

 >>> a =  " abc  "  >>> ID (a)  52053576L #   Reference the newly generated object  >>> B = a.replace ( " a   ", "  a   " )  > >> ID (b)  47950704l>>> ID (a)  52053576l>>> b   " abc   " 

Always keep in mind that a it is a variable, but a ‘abc‘ string Object! Sometimes, we often say that the object's a content is ‘abc‘ , but actually refers to, a itself is a variable, it points to the content of the object is ‘abc‘ :

When we call a.replace(‘a‘, ‘A‘) , the actual invocation method replace is on the string object ‘abc‘ , and although the method is called, it replace does not change ‘abc‘ the contents of the string. Instead, the replace method creates a new string ‘Abc‘ and returns, and if we point to the new string with a variable, b it's easy to understand that the variable a still points to the original string ‘abc‘ , but the variable points to the b new string ‘Abc‘ :

Therefore, for an immutable object, any method that invokes the object itself does not change the contents of the object itself. Instead, these methods create a new object and return it, ensuring that the immutable object itself is always immutable.

When you manipulate a mutable object (e.g list), it changes the contents of the object itself (in-place change).

>>> List1 = [6, 1, 5]>>> ID (list1)55271368l>>> list1.append (2) >>> ID (list1)55271368L

3 Tuple (tuple)

When you define a tuple, the elements of the tuple must be determined when defined.

Finally, let's look at a "mutable" tuple:

>>> t = ('a' b', ['a' ' B ' ])
>>> T
(' A ', ' B ', [' A ', ' B '])
>>> Hash (t)

Traceback (most recent):
File "<pyshell#179>", line 1, in <module>
Hash (t)
Typeerror:unhashable type: ' List '

' X ' ' Y '>>> t ('a'b', ['  X'Y'])

This tuple is defined by 3 elements, respectively ‘a‘ , ‘b‘ and a list. Doesn't it mean that once a tuple is defined, it's immutable? Why did you change it later?

Don't worry, let's take a look at the definition when the tuple contains 3 elements:

When we put the list element ‘A‘ and ‘B‘ modify ‘X‘ it to and ‘Y‘ after, the tuple becomes:

On the surface, the elements of a tuple do change, but in fact it is not a tuple element, but a list element. The list that the tuple initially points to is not changed to another list, so the so-called "invariant" of a tuple is that each element of a tuple is directed to never change. That is, pointing ‘a‘ , cannot be changed to point ‘b‘ , point to a list, can not be changed to point to other objects, but the list itself is variable!

After understanding "point to invariant", how do you create a tuple that does not change the content? It is important to ensure that each element of a tuple cannot be changed.

Reference:

Python source--A memory pool of integer objects (pyintobject)

Principle of Integer Object implementation

A preliminary study of Python's small integer object--Recommended blog

Using list and tuple

Python References and object understanding

Python immutable objects

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.