A brief analysis of references and copies in Python _python

Source: Internet
Author: User
Tags garbage collection shallow copy

If An object ' s value can be modified, the object was said to be mutable. If The value cannot be Modified,the object is said to be immutable.

mutable mutable types, such as List,set, custom types (equivalent to reference types in C #);

Immutable immutable types, such as string,numbers (equivalent to value types in C #);

I. References and copies (references and copies)

When using the = assignment operator in a program, such as A=b,

For immutable objects, a copy of A as B is created, A and B will point to different memory addresses, and A and b are independent of each other.

Copy Code code as follows:

Def testcopy ():
A = 10
b = A
A =20
Print (b) #b still is 10

But for mutable objects, a reference to B is created, and elements A and B share the same memory address, and the elements of A and B are shared.
Copy Code code as follows:

Def testref ():
a=[1,2,3,4]
B=a #b is a reference to a
Print (b is a) # True
B[2] = -100 #change an element in B
Print (a) # A also changed to [1,2,-100,4]

Second, deep and shallow copies (shallow copy and deep copy)

To prevent a mutable object from pointing to the same object, you must create a new copy, not a reference.
In Python, you can use two copies of container objects such as lists and dictionaries: shallow copies and deep copies.

A shallow copy creates a new object, but populates the new object with a reference to the element of the original object (if the invariant type is equivalent to a copy). You can use Copy.copy () to implement shallow copies.

Copy Code code as follows:

Def testshallowcopy ():
A = [1, 2, [3,4]]
b = List (a) # Create a shallow copy of a
Print (b is a) #False
B.append #append element to B
Print (b)
Print (a) # A is unchanged
B[2][0] = -100 # Modify an element inside B
Print (b)
Print (a) # A is changed

In this example, A and B share the same variable elements. So modify the elements in one of the list objects, and the other list object will be modified.

Deep copy creates a new object and recursively copies all the elements contained in the object. You can use Copy.deepcopy () to implement a deep copy.

Copy Code code as follows:

Def testdeepcopy ():
Import Copy
A = [1, 2, [3, 4]]
b = Copy.deepcopy (a)
B[2][0] =-100
Print (b) # B is changed
Print (a) # A is unchanged

In this example, A and B are the opposite list objects, and their elements are independent of each other.

Iii. reference count and garbage collection

All objects in Python are reference-counted, when an object is assigned or added to a container, its reference count increases, and when you use the DEL or variable assignment to another value, the reference count is decremented, and when the reference count is 0 o'clock, the python garbage collector reclaims the variable.

Copy Code code as follows:

Def testgarbagecollection ():
Import Sys
Print (Sys.getrefcount (37))
A = Panax creates an object with value 37
Print (Sys.getrefcount (37))
b = A # Increases reference count on 37
Print (Sys.getrefcount (37))
c = []
C.append (b) # increases reference count on 37
Print (Sys.getrefcount (37))
Del A # Decrease reference count of 37
Print (Sys.getrefcount (37))
b = # Decrease reference count of 37
Print (Sys.getrefcount (37))
C[0] = 2.0 # Decrease reference count of 37
Print (Sys.getrefcount (37))

Testgarbagecollection ()

The results of the operation are:

Copy Code code as follows:

11
12
13
14
13
12
11

Why do you have 11 references on the first? Who knows?

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.