Depth Copy
A deep copy is divided into two parts: a number and another part of the string is a list, a tuple, a dictionary, and other data types.
numbers and Strings
for数字and the字符串, assignments, shallow copies, and deep copies are meaningless because their values will always point to the same memory address.
# Importing Copy module >>> Import copy# define a variable var1>>> var1 = 123
# Output VAR1 memory address >>> ID (var1) 1347747440>>> var2 = var1
# VAR2 memory address and VAR1 same >>> ID (var2) 1347747440
# shallow copy >>> var3 = copy.copy (var1)
# VAR3 memory address and VAR1 same >>> ID (VAR3) 1347747440
# deep copy >>> VAR4 = Copy.deepcopy (var1) # VAR4 memory address and VAR1 same >>> ID (VAR4) 1347747440
Other data Types
var1 = {"K1": "1", "K2": 2, "K3": ["abc", 456]}
Assign Value
Assignment, just create a variable that points to the original memory address, such as:
>>> var1 = {"K1": "1", "K2": 2, "K3" : ["abc", 456]}>>> var2 = var1>>> ID (var1) 1937003361288>>> ID (var2) 1937003361288
:
650) this.width=650; "src=" Https://blog.ansheng.me/static/uploads/2016/12/1483017038.png "alt=" python-day04-01 " Style= "Border:0px;vertical-align:middle;"/>
Shallow Copy
# using a shallow copy >>> var2 = copy.copy (var1)
# Two variable memory address is different and Gt;>> ID (VAR1) 2084726354952>>> ID (var2) 2084730248008# But their element memory address is the same >>> ID (var1["k1"]) 2084726207464>>> ID (var2["k1"]) 2084726207464
:
650) this.width=650; "src=" Https://blog.ansheng.me/static/uploads/2016/12/1483017066.png "alt=" python-day04-02 " Style= "Border:0px;vertical-align:middle;"/>
Deep Copy
deep copy, recreate all the data in memory (excluding the last layer, That is, Python's internal optimization of strings and numbers)
# Copy the contents of var1 to var2>>> var2 = copy.deepcopy (var1) using a deep copy # var1 and VAR2 memory addresses are not the same >>> ID (VAR1) 1706383946760>>> ID (var2) 1706389852744
# var1 and var2 elements "K3" The memory address is not the same as the >>> ID (var1["K3"]) 1706389853576>>> ID (var2["K3"]) 1706389740744# var1 and var2 "K3" The memory address of the element is the same as the >>> ID (var1["K3"][1]) 1706383265744>>> ID (var2["K3"][1]) 1706383265744
:
650) this.width=650; "src=" Https://blog.ansheng.me/static/uploads/2016/12/1483017092.png "alt=" python-day04-03 " Style= "Border:0px;vertical-align:middle;"/>
This article from "A Candle" blog, declined reprint!
Python depth copy