I am a programming enthusiast and have recently putClaws reach out to Python programming。。。。。 I was confused by copy and deepcopy, but now I'm going to make a distinction between these two methods, one for shallow copying (copy) and one for deep copying (deepcopy).
First of all, let's talk about deepcopy, the so-called deep copy, here I understand is a complete copy and then become a new object, the copy of the object and the copied object does not have any relationship, no matter how to change each other do not affect each other.
Then a little bit about copy, here I'm going to talk about it in two categories, a copy function for dictionary data types and a copy function for copy packages.
First, the dictionary data type of the copy function, when a simple value replacement, the original dictionary and the copied dictionary does not affect each other, but when adding, deleting and other modification operations, the two will affect each other.
(1) Value substitution
import copy d = { 'name' : ['An','Assan'] } c = () dc = (d) d['name'] = ['an'] print c print d print dc
The results are as follows:
{'name': ['An', 'Assan']} {'name': ['an']} {'name': ['An', 'Assan']}
(2) Value modification
import copy d = { 'name' : ['An','Assan'] } c = () dc = (d) d['name'].append('shu') print c print d print dc
The results are as follows:
{'name': ['An', 'Assan', 'shu']} {'name': ['An', 'Assan', 'shu']} {'name': ['An', 'Assan']}
Second, the copy function in the copy package, whether to modify or replace the value of the two do not affect each other.
import copy seq = [1,2,3] seq1 = seq seq2 = (seq) seq3 = (seq) (4) seq2[2] = 5 print seq,seq1,seq2,seq3
The results are as follows:
[1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 5] [1, 2, 3]
In the above code, sql1 = seq is actually pointing to the same object address, using the same object reference.
summarize
Python Algorithm Tutorial ([Norway] Hetland) Chinese complete pdf scan version
https:///books/
Python core programming (3rd edition) (U.S. Wesley Chun) Chinese pdf full version
https:///books/
I hope you enjoy it and thank you friends for supporting my site!