The paper
- Direct assignment: essentially a reference (alias) to an object.
- Shallow copy: Copies the parent object, not the internal children of the object.
- Deepcopy:
copy
The moduledeepcopy
Method that copies both the parent object and its children.
Copy is to copy the original data. Random changes in the copied data will not affect the original data. That’s what deep copy is.
Direct assignment
In [1]: a = [11.22.33]
In [2]: b = a
In [3]: b
Out[3] : [11.22.33]
In [4] :id(a), id(b)
Out[4] : (2053851155016.2053851155016)
In [5]: c = {"name": "hui"}
In [6]: d = c
In [7] :id(c), id(d)
Out[7] : (2053851035112.2053851035112)
In [8]: a.append(44)
In [9]: a
Out[9] : [11.22.33.44]
In [10]: b
Out[10] : [11.22.33.44]
In [11]: c["age"] = 21
In [12]: c
Out[12] : {'name': 'hui'.'age': 21}
In [13]: d
Out[13] : {'name': 'hui'.'age': 21}
Copy the code
As you can see, when you assign a value to a variable directly, you’re actually making a copy of the object reference, so id() gets the same memory address, and they all refer to the same object. Let me draw a picture of it.
Shallow copy
Use the built-in module copy to implement shallow copy
In [12]: a = [1.2]
In [13]: b = [3.4]
In [14]: c = [a, b]
In [15]: d = c
In [16] :id(c), id(d)
Out[16] : (1409068540040.1409068540040)
In [17] :import copy
In [18]: e = c.copy()
In [19] :id(c), id(d), id(e)
Out[19] : (1409068540040.1409068540040.1409070776520)
The object id() of the shallow copy is different
In [20] :id(c[0]), id(c[1])
Out[20] : (1409071493512.1409071679112)
In [21] :id(e[0]), id(e[1])
Out[21] : (1409071493512.1409071679112)
The same goes for its children
In [22]: a.append(5)
In [23]: b.append(6)
In [24]: c
Out[24] : [[1.2.5], [3.4.6]]
In [25]: d
Out[25] : [[1.2.5], [3.4.6]]
In [26]: e
Out[26] : [[1.2.5], [3.4.6]]
# Consistent content
In [28]: c.append(7)
In [29]: c
Out[29] : [[1.2.5], [3.4.6].7]
In [30]: e
Out[30] : [[1.2.5], [3.4.6]]
Copy the code
It can be seen that the direct assignment c and D are the same object, while the shallow copy C and E are separate objects, but their child objects A and B still refer to the unified object, that is, reference.
So when c.append(7), only c object changes, while the shallow-copy E remains unchanged.
After a.append(5) and b.append(6), objects c, D, and e still have the same contents.
Deep copy
Deepcopy is implemented by copy.deepcopy()
In [33]: a = [1.2]
In [34]: b = [3.4]
In [35]: c = [a, b]
In [36]: d = copy.deepcopy(c)
In [37] :id(c), id(d)
Out[37] : (1409071919752.1409071607112)
In [38] :id(c[0]), id(c[1])
Out[38] : (1409071948680.1409071766216)
In [39] :id(d[0]), id(d[1])
Out[39] : (1409071976328.1409071919880)
The id() of the child object is different
In [40]: c.append(5)
In [41]: c
Out[41] : [[1.2], [3.4].5]
In [42]: d
Out[42] : [[1.2], [3.4]]
In [43]: a.append(3)
In [44]: b.append(5)
In [45]: c
Out[45] : [[1.2.3], [3.4.5].5]
In [46]: d
Out[46] : [[1.2], [3.4]]
So no matter how c is modified, d will not be affected
In [47]: d[0].append(5)
In [48]: d[1].append(6)
In [49]: d
Out[49] : [[1.2.5], [3.4.6]]
In [50]: d.append(7)
In [51]: d
Out[51] : [[1.2.5], [3.4.6].7]
In [52]: c
Out[52] : [[1.2.3], [3.4.5].5]
# d does not affect c
Copy the code
Deep copy, a complete copy of the parent object and its child object, the two are completely independent. So there’s no difference between c and D.
The three contrast
d = c
Assignment reference,c
和d
They all point to the same objecte = c.copy()
A shallow copy,c
和e
Is aIndependent objectBut theirThe child object still refers to the unified object, which is a reference.f = copy.deepcopy(c)
Deep copy,c
和f
The parent object and its child object are completely copied, and the two are completely independent.
Copy the difference between mutable and immutable types
-
Copy.copy () for mutable types, shallow copies are made.
-
Copy. Copy () for immutable types, it does not copy, it just points to.
-
Copy. Deepcopy () the deepcopy recursively copies all mutable and immutable types the same, and the objects are completely independent
By immutable, we mean that the contents of the pointed memory are immutable.
The same memory address, its contents have changed, but the address remains the same. Note Mutable data types such as list, set, and dict.
The data type | If the variable |
---|---|
The integer | immutable |
stringstr |
immutable |
tuplestuple |
immutable |
The list oflist |
variable |
A collection ofset |
variable |
The dictionarydict |
variable |
Shallow copy test
In [54] :List of mutable types
In [55]: a = [1.2.3]
In [56]: b = copy.copy(a)
In [57] :id(a)
Out[57] :1409069563528
In [58] :id(b)
Out[58] :1409071719752
In [59]: a.append(4)
In [60]: a
Out[60] : [1.2.3.4]
In [61]: b
Out[61] : [1.2.3]
In [63] :# immutable type tuple
In [64]: c = (1.2.3)
In [65]: d = copy.copy(c)
In [66] :id(c)
Out[66] :1409070834456
In [67] :id(d)
Out[67] :1409070834456
Copy the code
If a shallow copy of an immutable tuple of copy() has the same memory address, the memory address of tuple, c, and d is the same.
Deep copy test
In [71]: a = ([1.2], [3.4])
In [72]: b = copy.copy(a)
In [73]: c = copy.deepcopy(a)
In [74] :id(a)
Out[74] :1409068519944
In [75] :id(b)
Out[75] :1409068519944
# shallow copy immutable type ID () consistent
In [76] :id(c)
Out[76] :1409071533448
The deep copy immutable type id() is inconsistent
In [77]: b[0].append(3)
In [78]: b[1].append(5)
In [79]: a
Out[79]: ([1.2.3], [3.4.5])
In [80]: b
Out[80]: ([1.2.3], [3.4.5])
In [81]: c
Out[81]: ([1.2], [3.4])
Shallow-copy child object references are consistent
In [82]: c[0].append(3)
In [83]: c
Out[83]: ([1.2.3], [3.4])
In [84]: b
Out[84]: ([1.2.3], [3.4.5])
In [85]: a
Out[85]: ([1.2.3], [3.4.5])
Deep copy is a full copy, which does not affect each other
Copy the code
Copy. Deepcopy () the deepcopy recursively copies all mutable and immutable types the same, and the objects are completely independent.
The public,
Create a new folder X
Nature took tens of billions of years to create our real world, while programmers took hundreds of years to create a completely different virtual world. We knock out brick by brick with a keyboard and build everything with our brains. People see 1000 as authority. We defend 1024. We are not keyboard warriors, we are just extraordinary builders of ordinary world.