Example analysis of references and replication usages in Python

Source: Internet
Author: User
Tags shallow copy in python

The examples in this article describe references and replication usage in Python. Share to everyone for your reference. The specific analysis is as follows:

In Python, any immutable object is a value, and a Mutable object is a reference.

Immutable objects (such as integers, strings) are actually copied, whether they are passing arguments to a function or any form of object copying, and mutable objects simply copy a reference to them, that is, there is only one object in memory, and two copies are referenced.

A=b Such an assignment creates a reference to B, and for immutable objects such as numbers and strings, this assignment actually creates a copy of B

?

1 2 3 4 5 6 7 8 9 10 11 >>> a= ' Hello ' >>> b=a >>> ID (a) 29326432 >>> ID (b) 29326432 >>> B is a True & gt;>> a=1000 >>> B ' Hello '

For mutable objects, such as dictionaries and lists, A and B refer to the same object, and modifying any one of these variables affects the other.

?

1 2 3 4 5 6 7 8 9 10 >>> a=[1,2,3,4] >>> b=a >>> ID (a) 29280896 >>> ID (b) 29280896 >>> b[3]= ' CCCC CCCCC ' >>> A [1, 2, 3, ' CCCCCCCCC '] >>>

A container object such as a list and a dictionary can use two assignment operations: shallow copy and deep copy. Shallow copy creates a new object, but it contains a reference to the items contained in the original object.

For example, the following shallow copy:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 >>> a=[1,2,3,4,[9,0]] >>> b=a >>> a=[1,2,3,4,[9,0]] >>> b=list (a) >>> B is a False >>> b[0]=1000 >>> b [1000, 2, 3, 4, [9, 0]] #注意, b modified b[0] After, to a no effect >>> a [1, 2, 3, 4, [9, 0]] >>> b[4][1]= ' CCCC ' #注意, b modified b[4][1] After, to a has influence >>> b [1000, 2, 3, 4, [9, ' CCCC ']] >>> a [1, 2 , 3, 4, [9, ' CCCC ']]

Deep copy creates a new object and recursively replicates all the objects it contains, no built-in objects can create deep copy, and can be done using the copy.deepcopy () function.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 >>> import copy >>> a=[1,2,3,[4,5]] >>> b=copy.deepcopy (a) >>> ID (b) 29582240 >& Gt;> ID (a) 29581840 >>> A is b False >>> b[0]=1000 >>> b [1000, 2, 3, [4, 5]] #注意修改了b [0] after a No impact >>> A [1, 2, 3, [4, 5]] >>> b[3][1]= ' gggg ' >>> b [1000, 2, 3, [4, ' GGGG ']] after #修改了 b[3][1] A also has no effect, this is the difference with the shallow copy >>> a [1, 2, 3, [4, 5]]

I hope this article will help you with your Python programming.

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.