python--variable and immutable types

Source: Internet
Author: User

Mutable and immutable Type objects

Mutable and immutable, essentially changing the memory address After the data is changed

Variable type, non-hash Immutable type, can hash
List String
Dictionary Digital
Collection Meta-group
Immutable collections

  

  

What is a mutable/immutable object

An immutable object in which the object points to an in-memory value that cannot be changed. When a variable is changed, because the value it refers to cannot be changed, it is equivalent to copy the original value and then change, this will open a new address, the variable point to the new address.

A mutable object in which the object points to an in-memory value that can be changed. Variables (accurately referred to) after the change, is actually the value of its direct change, and did not occur replication behavior, nor open up new addresses, popular point is to change in situ.

Examples of immutable objects
A = 2b = 2c = A + 0 c + = 0print (ID (a), id (b), ID (2))  # The IDs are all the same print (c is b) #True   

Immutable conditions in a string

Astr = ' good 'BSTR = ' good 'CStr = astr + ' Print (CStr is BSTR) # Trueprint (ID (ASTR), ID (BSTR), ID (' good '))  # Three Same ID 
If this is the case, the variable is not modified after thegood
Astr = ' good ' print(ID (astr)) astr + = ' AA ' Print (ID (ASTR)) # ID is not the same as above

Because it is an immutable object, the value of the variable corresponding to the memory is not allowed to be changed. When the variable is going to change, it is actually copying the original value and then changing it, opening up a new address,

Astr point to this new address (so the ID of the ASTR is not the same), the original astr corresponding value because no longer have the object point to it, it will be garbage collected. This is the same for int and float types.

Look at the tuple again.

Add = (1, 2, 3)AEE = (1, 2, 3) print (ID (add), ID (AEE), id ((1, 2, 3))  # IDs are different Aee = (1, 2, 3) print( ID (AEE)) Aee + = () # Add empty tuple print (ID (AEE))  # ID changed! Print (AEE)  # (1, 2,3)    

Although it all seems (1 ,2, 3) to be supposed to be consistent with the above. Is this a mutable object? Look again

Add = (1, 2, 3) Aee = add print (ID (AEE), id (add)) # These two IDs are the same AEE + = (4, 5, 6) print (ID (AEE)) # AEE ID changed! Print (add) # Add or (1, 2, 3) does not change  

If it is a mutable object add = aee , they point to the same address (the same ID) is positive. The change of AEE does not cause the change of add, so the tuple is an immutable object, but it is slightly different from the STR value type. The commonly said tuple can not be changed many times refers to the stored value can not be changed (some special cases, such as a tuple inside the list, can change the elements in the list.) But in fact the tuple has not been changed).

For str、int、float as long as they are of the same type, the values are the same, and their IDs are the same. (Why would you say the same type?) )

A = 2.0B = 2print (A is B)  # False, an int is a float and the types are different
2 and 2.0 are not on one address.
Examples of mutable objects
Lis = [1, 2, 3]lis2 = [1, 2, 3]# Although their contents are the same, they point to different memory addresses print (Lis is lis2) print (LIS), ID (LIS2), id ([1, 2, 3])  # Three IDs are different  

And then look at the assignment.

Alist = [1, 2, 3]# alist is actually a reference to an object, blist = Alist is a reference to the pass, and now two references point to the same object (address) blist = alistprint (ID (alist), ID (blist )  # ID # So one of the changes will affect another blist.append (4) print (alist)  # change blist, Alist also becomes [1, 2, 3 4]print (ID (alist), ID (blist)) # ID is the same as the ID  when the face value has not changed  

The same is true for collections:

ABB = {1, 2, 3}ACC = abbprint(ID (ABB), ID (ACC)) Acc.add (4) print (ABB)  # {1, 2, 3, 4} print (ID (ABB), ID (a cc)) # equal   

It is consistent with the example above list.

Mutable objects because the object can be modified, so there is no need to duplicate a copy and then change, directly in situ changes, so will not open up new memory, change the ID and the same.

Of course the immutable object is not like this, can compare with this

ABC = 3dd = ABCDD = 43print (ABC)  # 3, does not change with DD 

But if it is a copy, it is simply copying the contents of the past, passing it and not referencing it. This is especially useful when you want to use the values of the list and don't want to modify the original list.

Blist = alist[:]  # or alist.copy () print (Alist is blist)  # Falseblist.append (4) print (alist)  # or [3] no change
About the same extension of memory address: small Data pool

In order to optimize speed, Python avoids frequent use of integers and occupies memory space. Integer objects in the "5,256" range for smaller integers are built in advance, are not garbage collected, and are common to a memory address

Comparison of Numbers:

>>> a= 1000 # using the Python editor, more than 256, the address is different >>> B = 1000>>> A is bFalse>>> a = 233& Gt;>> B = 233>>> A is bTrue

Comparison of strings:

When there are spaces in the string, the memory address is different >>> a = ' a b c ' >>> B = ' a b C ' >>> A is bfalse>>> a = ' a B ' & Gt;>> B = ' A B ' >>> A is bfalse>>> a = ' ab ' >>> b = ' ab ' >>> A is btrue when string length >2 0, memory address differs >>> a = ' s ' *20>>> b = ' s ' *20>>> A is btrue>>> a = ' s ' *21>>> b = ' s ' 1>>> A is Bfalse
Large integer Pool:

The terminal (cmd\) is re-created every time it is executed, and in Pycharm, each run is loaded into memory and belongs to a whole, so there is a concept of a large integer object pool, that is, a large integer in a block of code is the same object.

# C1 and D1 are in a block of code, equal # c1.b and c2.b have their own code block, not equal to the program in Pycharm: Class C1 (object):    a = +    B = +    c =    1000 Class D2 (object):    a = +    B =    1000print (c1.a is c1.b)  # trueprint (c1.a is D2. A)  # Tru E
Print (c1.a is D2. b) # True
Print (c1.c is c1.d) # True
d2.c) # False

python--variable and immutable types

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.