The examples in this paper describe Python shallow copy and deep copy usage. Share to everyone for your reference. The specific analysis is as follows:
>>> person=[' name ', [' savings ',100]]>>> hubby=person[:]>>> wifey=list (person) >> > [ID (x) for x in person,hubby,wifey][3074051788l, 3074061740L, 3074061996l]>>> [ID (y) for x in Person,hubby,w Ifey for y in x][3074319552l,3073979916l,3074319552l,3073979916l,3074319552l,3073979916l]>>> hubby[0]= ' Joe ' >>> wifey[0]= ' Jane ' ([' Joe ', [' savings ', 100]],[' Jane ', [' savings ', ' 100]],[' name ', [' savings ',]]) > >> hubby[1][1]=70>>> Hubby,wifey,person ([' Joe ', [' savings ', 70]],[' Jane ', [' savings ', 70]],[' name ', [' Savings ', 70]])
A shallow copy creates a new object, but its object reference is exactly the same as the copied object.
If the constituent element of the copied object is an immutable type, the Copy object changes to this element, in effect creating a new object.
If the constituent element of the copied object is a mutable type, the copy object changes the element, which in effect modifies the contents of the memory space on the memory space of the original object.
In this way, only hubby[1][1]=70 can cause changes in the corresponding values of Wifey,person.
Hopefully this article will help you with Python programming.