Python's shallow copy deep copy

Source: Internet
Author: User
Tags shallow copy

I have not understood the shallow copy in Python and deep copy is a bird meaning, through a period of learning, finally understand some, so write to let oneself deepen the impression, and share and communicate with you!

# Shallow Copy command copy.copy (x)     Return a shallow copy of X. Return a shallow copy x# deep Copy command copy.deepcopy (x) return a deep copy of X. Return a dark copy x shallow copy Creates a new object and inserts it into the object or reference associated with it. A deep copy also creates a new object, but it is a recursive copy of the object and is inserted into the relevant object or the reference person. There are two problems with deep copy, what is it?
1. Recursive copying of objects may result in cyclic recursion.
2, because the deep copy of the copy of everything, so the copy of the object too many, the data management structure will be shared among each other.
What do you mean by these two words? It's too hard to understand, but examples will make us understand a lot     
>>>a ={' A ': 1, ' C ': {' E ': 4, ' d ': 3}, ' B ': 2, ' F ': 5
}>>> A
{'a ': 1, 'C': {'e': 4, 'd ': 3}, 'b': 2, 'f': 5} #输出结果
>>>b = Copy.copy (a) # a shallow copy to b=
>>> b
{ " a ' : 1, " c : {" e : 4, d : 3}, b : 2, " f : 5} # >>> a['a']="2" #我把a给更改 A bit
>>> A
{ , ' c : { ' e : 4, ' d : 3}, " b : 2, " f : 5 } #这是更改完后的结果, A=1 became a=2.
< EM id= "__mcedel" > >>> b
{ /span> a : 1, c : { E : 4, " d : 3}, " b : 2, " f : 5 } #此时的b a=1 unchanged, don't worry. Change a again, but change the different parts
>>> a['C'['e']=100 #更改a
>>> A
{ " Span style= "color: #800000;" >a : " , C : { E : " d : 3}, " b : 2, " f : 5 } #输出更改后的结果
< EM id= "__mcedel" > >>> b
{ /span> a : 1, c : { E : " d : 3}, " b : 2, " f : 5 } #现在再看看b的结果, gee, just a change, b unchanged, And now it's a change, why? Hey, right, this is the so-called shallow copy principle Ah, see another example
>>>a = [' A ', ' B ', 1,[' C ', ' 2 '], ' d ']
>>> A [' A ', ' B ', 1, [' C ', ' 2 '], ' d ']
>>> b = copy.copy (a)
>>> b
[' A ', ' B ', 1, [' C ', ' 2 '], ' d ']
also wrote a list of examples, I want to say is actually a dictionary above is the same, good, and then said shallow copy, as the name implies to think, shallow is the surface chant, look at the above example can know, just the list (dictionary) of the surface that layer copy (that is, copy a copy to B), Inside the nested list in fact A and B are still common, did not say to copy a copy out to B, the surface of the layer list in memory and open up a piece of memory space, and nested that layer list does not move.
#Deep Copy>>> C =Copy.deepcopy (a)>>>a{'a':'2','C': {'e': 100,'D': 3},'b': 2,'F': 5}>>>c{'a':'2','C': {'e': 100,'D': 3},'b': 2,'F': 5}>>> c['a']=20>>>c{'a': 20,'C':"{' E ': +, ' d ': 3}",'b': 2,'F': 5}>>>a{'a':'2','C': {'e': 100,'D': 3},'b': 2,'F': 5}>>> c['C']['e']=200>>>c{'a':'2','C': {'e': 200,'D': 3},'b': 2,'F': 5}>>>a{'a':'2','C': {'e': 100,'D': 3},'b': 2,'F': 5}
#深拷贝就不多说啦, it is recursive, that is, no matter how many lists (dictionaries) you nest, it will give you a copy of it to others (whatever the shallow layer of the deep object will open up new memory space for them, but the type is constant, The so-called type is the original object is a list (dictionary) shallow copy or deep copy, still a list (dictionary))

Python's shallow copy deep copy

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.