浅拷贝

 1 # 浅拷贝只copy一层
 2 
 3 s = [3,'Lucy',4,[1,2]]
 4 s1 = s.copy()
 5 print(id(s))    # 80065200
 6 print(id(s1))   # 80007336
 7 print(id(s[1]))     # 78244096
 8 print(id(s1[1]))    # 78244096
 9 print(id(s[3][1]))      # 494851904
10 print(id(s1[3][1]))     # 494851904

深拷贝

 

 1 # 深拷贝——克隆一分
 2 import copy
 3 
 4 s = [3,'Lucy',4,[1,2]]
 5 s2 = copy.deepcopy(s)
 6 
 7 s2[2] = 5
 8 s[3][1] = 6
 9 print(s)        # [3, 'Lucy', 4, [1, 6]]
10 print(s2)       # [3, 'Lucy', 5, [1, 2]]
11 
12 print(id(s))    # 81312784
13 print(id(s2))   # 81311504
14 
15 print(id(s[0]))     # 495703888
16 print(id(s2[0]))    # 495703888
17 
18 print(id(s[2]))     # 501405536
19 print(id(s2[2]))    # 501405552
20 print(id(s[3][1]))      # 501405568
21 print(id(s2[3][1]))     # 501405504

 

 

 

posted on 2018-03-26 12:46  &&||!  阅读(149)  评论(0编辑  收藏  举报