|
| 1 | +## 深拷贝与浅拷贝 |
| 2 | +#### 不可变变量 |
| 3 | +```python |
| 4 | +# 对于Python中的不可变变量(数值,字符串,元组),赋值,浅拷贝与深拷贝都是一样的,都是引用地址。 |
| 5 | +# a = 123 ,变量a表示的是123在内存中的地址 |
| 6 | +# b = a,就是把b也指向123在内存中的地址 |
| 7 | +# copy.copy(a),浅拷贝复制的也是123在内存中的地址 |
| 8 | +# copy.deepcopy(a),深拷贝复制的也是123在内存中的地址 |
| 9 | +# 所以 a,b,c,d都指向内存中同一个内存地址,他们的值都是123,他们的id(可以看做内存地址)相等。 |
| 10 | +a = 123 |
| 11 | +b = a |
| 12 | +c = copy.copy(a) |
| 13 | +d = copy.deepcopy(a) |
| 14 | +print('a: {}, id={}'.format(a, id(a))) # a: 123, id=1625849120 |
| 15 | +print('b: {}, id={}'.format(b, id(b))) # b: 123, id=1625849120 |
| 16 | +print('c: {}, id={}'.format(c, id(c))) # c: 123, id=1625849120 |
| 17 | +print('d: {}, id={}'.format(d, id(d))) # d: 123, id=1625849120 |
| 18 | + |
| 19 | +# a = 100,变量a由原本指向内存中123所在的内存地址,变为指向内存中100所在的内存地址 |
| 20 | +# 所以变量a表示的内存地址变化,b,c,d还是指向123所在的内存地址,并没有发生变化 |
| 21 | +a = 100 |
| 22 | +print('a: {}, id={}'.format(a, id(a))) # a: 100, id=1625848384 |
| 23 | +print('b: {}, id={}'.format(b, id(b))) # b: 123, id=1625849120 |
| 24 | +print('c: {}, id={}'.format(c, id(c))) # c: 123, id=1625849120 |
| 25 | +print('d: {}, id={}'.format(d, id(d))) # d: 123, id=1625849120 |
| 26 | +``` |
| 27 | +#### 可变变量 |
| 28 | +```python |
| 29 | +# 无论是不可变变量还是可变变量 |
| 30 | +# = 赋值都是表示对象的引用传递 |
| 31 | +a = {'k1': 123, 'k2': [4, 5, 6]} |
| 32 | +b = a |
| 33 | +# a: {'k1': 123, 'k2': [4, 5, 6]}, id=2036596592712 |
| 34 | +# b: {'k1': 123, 'k2': [4, 5, 6]}, id=2036596592712 |
| 35 | +print('a: {}, id={}'.format(a, id(a))) |
| 36 | +print('b: {}, id={}'.format(b, id(b))) |
| 37 | + |
| 38 | +a['k1'] = 100 |
| 39 | +# a: {'k1': 100, 'k2': [4, 5, 6]}, id=2036596592712 |
| 40 | +# b: {'k1': 100, 'k2': [4, 5, 6]}, id=2036596592712 |
| 41 | +print('a: {}, id={}'.format(a, id(a))) |
| 42 | +print('b: {}, id={}'.format(b, id(b))) |
| 43 | +print('b: {} |
| 44 | +``` |
| 45 | +#### copy.copy 浅拷贝 |
| 46 | + |
| 47 | +```python |
| 48 | +# 浅拷贝,只拷贝对象里面的不可变变量,对于对象里面的可变变量,仍然是引用传递 |
| 49 | +a = {'k1': 123, 'k2': [4, 5, 6]} |
| 50 | +c = copy.copy(a) |
| 51 | +# a: {'k1': 123, 'k2': [4, 5, 6]}, id=2067138400400 |
| 52 | +# c: {'k1': 123, 'k2': [4, 5, 6]}, id=2067138400328 |
| 53 | +print('a: {}, id={}'.format(a, id(a))) |
| 54 | +print('c: {}, id={}'.format(c, id(c))) |
| 55 | + |
| 56 | +a['k1'] = 100 |
| 57 | +# a: {'k1': 100, 'k2': [4, 5, 6]}, id=2067138400400 |
| 58 | +# c: {'k1': 123, 'k2': [4, 5, 6]}, id=2067138400328 |
| 59 | +print('a: {}, id={}'.format(a, id(a))) |
| 60 | +print('c: {}, id={}'.format(c, id(c))) |
| 61 | + |
| 62 | +# 浅拷贝对于对象里面的可变变量,仍然是引用传递,所以k2的列表会一起跟着变化 |
| 63 | +a['k2'][0] = 0 |
| 64 | +# a: {'k1': 100, 'k2': [0, 5, 6]}, id=2067138400400 |
| 65 | +# c: {'k1': 123, 'k2': [0, 5, 6]}, id=2067138400328 |
| 66 | +print('a: {}, id={}'.format(a, id(a))) |
| 67 | +print('c: {}, id={}'.format(c, id(c))) |
| 68 | +``` |
| 69 | + |
| 70 | +#### copy.deepcopy() 深拷贝 |
| 71 | +```python |
| 72 | +# 深拷贝,完全拷贝了对象及其子对象,没有任何引用传递 |
| 73 | +# 深拷贝后的对象与原对象是完全独立的,其中一方的修改不会影响另一方 |
| 74 | +a = {'k1': 123, 'k2': [4, 5, 6]} |
| 75 | +d = copy.deepcopy(a) |
| 76 | +# a: {'k1': 123, 'k2': [4, 5, 6]}, id=3069791555728 |
| 77 | +# d: {'k1': 123, 'k2': [4, 5, 6]}, id=3069794267424 |
| 78 | +print('a: {}, id={}'.format(a, id(a))) |
| 79 | +print('d: {}, id={}'.format(d, id(d))) |
| 80 | + |
| 81 | +a['k2'][0] = 0 |
| 82 | +# a: {'k1': 123, 'k2': [0, 5, 6]}, id=3069791555728 |
| 83 | +# d: {'k1': 123, 'k2': [4, 5, 6]}, id=3069794267424 |
| 84 | +print('a: {}, id={}'.format(a, id(a))) |
| 85 | +print('d: {}, id={}'.format(d, id(d))) |
| 86 | +``` |
0 commit comments