object reference, shallow copy, and deep copy in Python

Source: Internet
Author: User

The recent project encountered a Python shallow copy mechanism caused by the bug, because of Python object reference, assignment, shallow copy/deep copy mechanism does not have enough knowledge, resulting in debugging a long time to find the problem, here is a simple record of the relevant concepts.

In Python's design philosophy, everything in Python is an object, and there is a ob_refcnt variable that maintains a reference count of objects, which determines the creation and extinction of objects.

So in a Python program, the general assignment operation is simply a reference to an rvalue, and does not create a new object, you can see the unique identifier of the object in Python by using the ID function, for example, the list object, the following code:

>>> alist=[[1,2],3,4]>>> blist=alist#alist/ Blist actually references the same list object in memory 140357688098184140357688098184>>> blist.append (5)>>>  blist[[1, 2], 3, 4, 5]>>> alist[[# since the actual reference to the same list object, Blist adds an element, The alist is actually exactly the same >>> ID (alist); ID (blist)140357688098184140357688098184

In the above code, the value of the alist is assigned to blist, in fact, only blist point to the alist in memory objects, both refer to the same list object, if the blist append a new element, because it is pointing to the same object, The output of the alist will change as well.

A shallow copy can be achieved through the slice syntax or copy function of the copy module--

>>>ImportCopy>>> alist=[[1,2],3,4]>>> blist=alist[:]>>> clist=copy.copy (alist)>>> ID (alist); ID (blist); ID (clist)#Alist/blist/clist has actually pointed to a different list object in memory140357691858696140357691897864140357720939912>>> ID (alist[0]); ID (blist[0]); ID (clist[0])#Alist[0]/blist[0]/clist[0] Three sub-objects still point to the same list object in memory140357691897800140357691897800140357691897800>>> Blist.append (5)>>>blist[[1, 2], 3, 4, 5]>>>alist[[1, 2], 3, 4]#changes to Blist object values no longer affect alist and CList>>>clist[[1, 2], 3, 4]>>> Alist[0].append ('a')>>>alist[[1, 2,'a'], 3, 4]>>>blist[[1, 2,'a'], 3, 4, 5]#because the actual reference to the same object, Alist[0] Sub-object value changes, will also be reflected from the blist[0]/clist[0]>>>clist[[1, 2,'a'], 3, 4]>>> ID (alist[1]); ID (blist[1]); ID (clist[1])109194881091948810919488

You can see that blist and CList are already new list objects and no longer refer to the Alist list object, but the child objects in the three list are the same references, because shallow copies in Python can only copy the parent object, not the child objects inside the object.

Deep copy can be achieved through the Copy.deepcopy function in the Copy module:

>>> alist=[[1,2],3,4]>>> blist=copy.deepcopy (alist)>>> ID (alist); ID (blist)#Alist/blist has referenced a different list object in memory140357692023560140357691897608>>> Blist.append (5)>>>blist[[1, 2], 3, 4, 5]>>>alist[[1, 2], 3, 4]#changes to the blist value do not affect the alist>>> ID (alist[0]); ID (blist[0])#Alist{0]/blist[0] Two sub-objects have also referenced a different list object in memory140357691897864140357691896136>>> Alist[0].append ('a')>>>alist[[1, 2,'a'], 3, 4]>>>blist[[1, 2], 3, 4, 5]#Alist[0] Sub-object value changes, and will no longer be impressed with the value of blist[0]>>> ID (alist[1]); ID (blist[1])1091948810919488

As you can see, alist and blist point to different list objects, while their sub-objects alist[0]/blist[0] Also point to different list objects after copying through copy.deepcopy, but Alist[1]/blist[1] Still point to the same object, because 3, 4 in Python is actually immutable object, equivalent to a constant, in Python only one by one copies of immutable objects, so regardless of the shallow copy/deep copy, is a reference to the same immutable object.

For dict/set of these Python type objects, there will also be a similar shallow copy/deep copy of the problem, the following dict as an example to paste the code:

Reference assignment:

>>> adct={'D': {1:2}, 3:4}>>> bdct=ADCT>>> ID (ADCT); ID (BDCT)#ADCT/BDCT actually referencing the same Dict object in memory140357688090760140357688090760>>> ID (adct['D']); ID (bdct['D'])#adct[' d ']/bdct[' d '] two sub-objects actually refer to the same Dict object in memory140357691897928140357691897928>>> bdct['D'].update ({'a':'b'})>>>bdct{'D': {1:2,'a':'b'}, 3:4}>>>adct{'D': {1:2,'a':'b'}, 3:4}#changes to the value of bdct[' d ' will directly affect the value of ADCT because it actually points to the same child object

Copy.copy Shallow copy:

>>> adct={'D': {1:2}, 3:4}>>> bdct=copy.copy (ADCT)>>> ID (ADCT); ID (BDCT)#ADCT/BDCT referencing different dict objects140357688082888140357720937544>>> ID (adct['D']); ID (bdct['D'])#adct[' d ']/bdct[' d '] two sub-objects still point to the same Dict object in memory140357688101704140357688101704>>> bdct['D'].update ({'a':'b'})>>>bdct{'D': {1:2,'a':'b'}, 3:4}>>>adct{'D': {1:2,'a':'b'}, 3:4}#changes to bdct[' d ' Sub-object values directly affect the value of ADCT because the same child object is actually referenced

Copy.deepcopy Deep Copy:

>>> adct={'D': {1:2}, 3:4}>>> bdct=copy.deepcopy (ADCT)>>> ID (ADCT); ID (BDCT)#ADCT/BDCT itself already refers to different Dict objects140357691897928140357688094152>>> ID (adct['D']); ID (bdct['D'])#ADCT/BDCT Sub-objects reference different Dict sub-objects140357688090760140357688085896>>> bdct['D'].update ({'a':'b'})>>>bdct{'D': {1:2,'a':'b'}, 3:4}>>>adct{'D': {1:2}, 3:4}#bdct[' d ' Sub-object changes no longer affect the value of adct[' d ']

object reference, shallow copy, and deep copy in Python

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.