Python学习笔记3 (深拷贝、浅拷贝)

 1 # -*- coding:utf-8 -*-
 2 # Author: Xiang
 3 
 4 import copy
 5 
 6 '''
 7 一、深拷贝、浅拷贝
 8 Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块。
 9 1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象,效果等同于list.copy()
10 2. copy.deepcopy 深拷贝 拷贝对象及其子对象
11 对于一般的浅拷贝,使用copy.copy就可以了
12 要复制列表L,使用list(L),
13 要复制一个字典d,使用dict(d),
14 要复制一个集合s,使用set(s),
15 可以总结出一个规律,
16 如果要复制一个对象o,它属于内建的类型t,那么可以使用t(o)来获得一个拷贝.
17 dict也提供了一个复制版本,dict.copy,这个和dict(d)是一样,
18 推荐使用后者,这个使得代码更一致,而且还少几个字符.
19 拷贝对象(以列表为例)有五种方法,作用各不相同,如下面例子所示
20 
21 二、is、==
22 Python中的对象包含三要素:id、type、value
23 其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值
24 is 判断的是a对象是否就是b对象,是通过id来判断的
25 == 判断的是a对象的值是否和b对象的值相等,是通过value来判断的
26 '''
27 
28 a = ['a','b','c',[1,2,3]]
29 b = a
30 c = a.copy()
31 d = copy.copy(a)
32 e = copy.deepcopy(a)
33 
34 print('a:',a)   #a: ['a', 'b', 'c', [1, 2, 3]]
35 print('b:',b)   #b: ['a', 'b', 'c', [1, 2, 3]]
36 print('c:',c)   #c: ['a', 'b', 'c', [1, 2, 3]]
37 print('e:',d)   #e: ['a', 'b', 'c', [1, 2, 3]]
38 print('d:',e)   #d: ['a', 'b', 'c', [1, 2, 3]]
39 print('*********************************************')
40 print('id of a:',id(a))     #id of a: 3236221905160
41 print('id of b:',id(b))     #id of b: 3236221905160
42 print('id of c:',id(c))     #d of c: 3236221905672
43 print('id of d:',id(d))     #id of d: 3236220607752
44 print('id of e:',id(e))     #id of e: 3236221906440
45 print('*********************************************')
46 print('b = a ?', b == a)    #b = a ? True
47 print('c = a ?', c == a)    #c = a ? True
48 print('d = a ?', d == a)    #d = a ? True
49 print('e = a ?', e == a)    #e = a ? True
50 print('*********************************************')
51 a.insert(3,'d')
52 print('\n')
53 print('a:',a)   #a: ['a', 'b', 'c', 'd', [1, 2, 3]]
54 print('b:',b)   #b: ['a', 'b', 'c', 'd', [1, 2, 3]]
55 print('c:',c)   #c: ['a', 'b', 'c', [1, 2, 3]]
56 print('e:',d)   #e: ['a', 'b', 'c', [1, 2, 3]]
57 print('d:',e)   #d: ['a', 'b', 'c', [1, 2, 3]]
58 print('*********************************************')
59 print('id of a:',id(a))     #id of a: 3236221905160
60 print('id of b:',id(b))     #id of b: 3236221905160
61 print('id of c:',id(c))     #id of c: 3236221905672
62 print('id of d:',id(d))     #id of d: 3236220607752
63 print('id of e:',id(e))     #id of e: 3236221906440
64 print('*********************************************')
65 print('b = a ?', b == a)    #b = a ? True
66 print('c = a ?', c == a)    #c = a ? False
67 print('d = a ?', d == a)    #d = a ? False
68 print('e = a ?', e == a)    #e = a ? False
69 print('*********************************************')
70 a[4].insert(3,4)
71 print('\n')
72 print('a:',a)   #a: ['a', 'b', 'c', 'd', [1, 2, 3, 4]]
73 print('b:',b)   #b: ['a', 'b', 'c', 'd', [1, 2, 3, 4]]
74 print('c:',c)   #c: ['a', 'b', 'c', [1, 2, 3, 4]]
75 print('e:',d)   #e: ['a', 'b', 'c', [1, 2, 3, 4]]
76 print('d:',e)   #d: ['a', 'b', 'c', [1, 2, 3]]
77 print('*********************************************')
78 print('id of a:',id(a))     #id of a: 3236221905160
79 print('id of b:',id(b))     #id of b: 3236221905160
80 print('id of c:',id(c))     #id of c: 3236221905672
81 print('id of d:',id(d))     #id of d: 3236220607752
82 print('id of e:',id(e))     #id of e: 3236221906440
83 print('*********************************************')
84 print('b = a ?', b == a)    #b = a ? True
85 print('c = a ?', c == a)    #c = a ? False
86 print('d = a ?', d == a)    #d = a ? False
87 print('e = a ?', e == a)    #e = a ? False
88 print('*********************************************')

原文地址:http://blog.csdn.net/grantlee1988/article/details/8095338

posted on 2017-06-13 09:42  小温xy  阅读(72)  评论(0)    收藏  举报