python找茬系列02--浅拷贝和深拷贝,十个人有八个人会晕!
2019-07-24 本文已影响0人
young十三
一、概念
可变数据类型:列表list和字典dict。
不可变数据类型:整型int、浮点型float、字符串型string和元组tuple。
一、实例分析
1、可变数据类型赋值
raw = [1, 2, 3, 4]
raw.append(5)
a = raw
print('raw = ', raw)
print('a = ', a)
a.append(6)
print('raw = ', raw)
print('a = ', a)
输出结果:
raw = [1, 2, 3, 4, 5]
a = [1, 2, 3, 4, 5]
raw = [1, 2, 3, 4, 5, 6]
a = [1, 2, 3, 4, 5, 6]
可变数据赋值过程.png
结论:当给可变数据类型直接赋值时,无论赋值的对象还是被赋值的对象发生改变,两者都同时变化
2、不可变数据类型赋值
raw = 10
a = raw
print(raw)
print(a)
raw = 5
print(raw)
print(a)
a = 15
print(raw)
print(a)
输出结果:
10
10
5
10
5
15
不可变数据赋值过程.png
当给不可变数据赋值时,无论被赋值对象还是赋值对象都互不影响对方
3、浅拷贝
I、浅拷贝切片方式
raw = [1, 2, 3, 4]
a = raw[:]
print(raw)
print(a)
print("==============")
a.append(5)
print(raw)
print(a)
print("==============")
raw.append(6)
print(raw)
print(a)
输出结果:
[1, 2, 3, 4]
[1, 2, 3, 4]
==============
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
==============
[1, 2, 3, 4, 6]
[1, 2, 3, 4, 5]
图.png
II、浅拷贝切片方式:调用copy模块
raw = [1, 2, 3, 4]
a = copy.copy(raw)
print(raw)
print(a)
print("==============")
a.append(5)
print(raw)
print(a)
print("==============")
raw.append(6)
print(raw)
print(a)
输出结果:
[1, 2, 3, 4]
[1, 2, 3, 4]
==============
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
==============
[1, 2, 3, 4, 6]
[1, 2, 3, 4, 5]
III、浅拷贝为什么叫浅拷贝,请看下面例子
import copy
raw = [1, 2, 3, 4, ['a', 'b']]
a = copy.copy(raw)
print(raw)
print(a)
print("==============================")
raw[4].append('c')
print(raw)
print(a)
print("==============================")
a[4].append('d')
print(raw)
print(a)
print("==============================")
输出结果:
[1, 2, 3, 4, ['a', 'b']]
[1, 2, 3, 4, ['a', 'b']]
==============================
[1, 2, 3, 4, ['a', 'b', 'c']]
[1, 2, 3, 4, ['a', 'b', 'c']]
==============================
[1, 2, 3, 4, ['a', 'b', 'c', 'd']]
[1, 2, 3, 4, ['a', 'b', 'c', 'd']]
==============================
图.png
import copy
raw = [1, 2, 3, 4, ['a', 'b']]
a = copy.copy(raw)
print(raw)
print(a)
print("==============================")
raw.append(5)
print(raw)
print(a)
print("==============================")
a.append(6)
print(raw)
print(a)
print("==============================")
输出结果:
[1, 2, 3, 4, ['a', 'b']]
[1, 2, 3, 4, ['a', 'b']]
==============================
[1, 2, 3, 4, ['a', 'b'], 5]
[1, 2, 3, 4, ['a', 'b']]
==============================
[1, 2, 3, 4, ['a', 'b'], 5]
[1, 2, 3, 4, ['a', 'b'], 6]
==============================
拷贝父对象,不会拷贝对象的内部的子对象
4、深拷贝为什么叫深拷贝,请看下面例子
import copy
raw = [1, 2, 3, 4, ['a', 'b']]
a = copy.deepcopy(raw)
print(raw)
print(a)
print("==============================")
raw.append(5)
print(raw)
print(a)
print("==============================")
a.append(6)
print(raw)
print(a)
print("==============================")
输出结果:
[1, 2, 3, 4, ['a', 'b']]
[1, 2, 3, 4, ['a', 'b']]
==============================
[1, 2, 3, 4, ['a', 'b'], 5]
[1, 2, 3, 4, ['a', 'b']]
==============================
[1, 2, 3, 4, ['a', 'b'], 5]
[1, 2, 3, 4, ['a', 'b'], 6]
==============================
import copy
raw = [1, 2, 3, 4, ['a', 'b']]
a = copy.deepcopy(raw)
print(raw)
print(a)
print("==============================")
raw[4].append('c')
print(raw)
print(a)
print("==============================")
a[4].append('d')
print(raw)
print(a)
print("==============================")
输出结果:
[1, 2, 3, 4, ['a', 'b']]
[1, 2, 3, 4, ['a', 'b']]
==============================
[1, 2, 3, 4, ['a', 'b', 'c']]
[1, 2, 3, 4, ['a', 'b']]
==============================
[1, 2, 3, 4, ['a', 'b', 'c']]
[1, 2, 3, 4, ['a', 'b', 'd']]
==============================
图.png
copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象。
二、赠语
黑发不知勤学早,白首方悔读书迟。