理解Python浅拷贝和深拷贝的区别

  • 原创
  • Madman
  • /
  • /
  • 1
  • 9291 次阅读

理解Python浅拷贝和深拷贝的区别-min.png

Synopsis: Python赋值操作或函数参数传递,传递的永远是对象引用(即内存地址),而不是对象内容。在Python中一切皆对象,对象又分为可变(mutable)和不可变(immutable)两种类型。对象拷贝是指在内存中创建新的对象,产生新的内存地址。当顶层对象和它的子元素对象全都是immutable不可变对象时,不存在被拷贝,因为没有产生新对象。浅拷贝(Shallow Copy),拷贝顶层对象,但不会拷贝内部的子元素对象。深拷贝(Deep Copy),递归拷贝顶层对象,以及它内部的子元素对象

1. 可变对象与不可变对象

Python中一切皆对象,对象就像一个塑料盒子, 里面装的是数据。对象有不同类型,例如布尔型和整型,类型决定了可以对它进行的操作。现实生活中的"陶器"会暗含一些信息(例如它可能很重且易碎,注意不要掉到地上)。

对象的类型还决定了它装着的数据是允许被修改的变量(可变的mutable)还是不可被修改的常量(不可变的immutable)。你可以把不可变对象想象成一个透明但封闭的盒子:你可以看到里面装的数据,但是无法改变它。类似地,可变对象就像一个开着口的盒子,你不仅可以看到里面的数据,还可以拿出来修改它,但你无法改变这个盒子本身,即你无法改变对象的类型。

  • mutable : 可变对象,如ListDict
  • immutable : 不可变对象,如NumberStringTupleFrozenset

注意: Python赋值操作或函数参数传递,传递的永远是对象引用(即内存地址),而不是对象内容。

In [1]: a = 1

In [2]: b = a

In [3]: id(a)
Out[3]: 9164864

In [4]: id(b)
Out[4]: 9164864

In [5]: b += 1

In [6]: a
Out[6]: 1

In [7]: b
Out[7]: 2

In [8]: id(a)  # 对象引用a还是指向Number对象1
Out[8]: 9164864

In [9]: id(b)  # 对象引用b指向了Number对象2
Out[9]: 9164896

说明:

Python会缓存使用非常频繁的小整数-5至256ISO/IEC 8859-1单字符只包含大小写英文字母的字符串,以对其复用,不会创建新的对象:

1. 不会创建新对象
In [1]: a = 10

In [2]: b = 10

In [3]: id(a)
Out[3]: 9165152

In [4]: id(b)
Out[4]: 9165152

In [5]: a = '@'

In [6]: b = '@'

In [7]: id(a)
Out[7]: 139812844740424

In [8]: id(b)
Out[8]: 139812844740424

In [9]: a = 'HELLOWORLDhelloworld'

In [10]: b = 'HELLOWORLDhelloworld'

In [11]: id(a)
Out[11]: 139812785036792

In [12]: id(b)
Out[12]: 139812785036792
2. 会创建新的对象
In [1]: a = 1000

In [2]: b = 1000

In [3]: id(a)
Out[3]: 140528314730384

In [4]: id(b)
Out[4]: 140528314731824

In [5]: a = 'x*y'

In [6]: b = 'x*y'

In [7]: id(a)
Out[7]: 139897777405880

In [8]: id(b)
Out[8]: 139897777403808

In [9]: a = 'Hello World'

In [10]: b = 'Hello World'

In [11]: id(a)
Out[11]: 139897789146096

In [12]: id(b)
Out[12]: 139897789179568

2. copy模块

对象拷贝是指在内存中创建新的对象,产生新的内存地址。

  • 浅拷贝只拷贝最外层对象,深拷贝还会递归拷贝内层对象

  • 无论是浅拷贝还是深拷贝,只拷贝mutable可变对象成为一个新对象,而immutable不可变对象还是原来的那个

  • 当顶层对象和它的子元素对象全都是immutable不可变对象时,因为没有产生新对象,所以不存在被拷贝

2.1 浅拷贝

浅拷贝(Shallow Copy),拷贝顶层对象,但不会拷贝内部的子元素对象。

(1) 顶层是mutable,子元素全是immutable

当顶层对象是mutable可变对象,但是它的子元素对象全都是immutable不可变对象时,如[1, 'world', 2]

① 创建列表对象并赋值给变量a

In [1]: a = [1, 'world', 2]

In [2]: [ id(item) for item in a ]
Out[2]: [9164864, 140104749066928, 9164896]

In [3]: id(a)
Out[3]: 140104759916040

② 导入copy模块,使用copy.copy()函数浅拷贝a,并赋值给变量b

In [4]: import copy

In [5]: b = copy.copy(a)

In [6]: b
Out[6]: [1, 'world', 2]

In [7]: [ id(item) for item in b ]
Out[7]: [9164864, 140104749066928, 9164896]

In [8]: id(b)
Out[8]: 140104760027784

③ 修改变量a的子元素a[0] = 3,由于整数是不可变对象,所以并不是修改1变为3,而是更改a[0]指向对象3

In [9]: a[0] = 3

In [10]: a
Out[10]: [3, 'world', 2]

In [11]: b
Out[11]: [1, 'world', 2]

In [12]: [ id(item) for item in a ]
Out[12]: [9164928, 140104749066928, 9164896]

In [13]: [ id(item) for item in b ]
Out[13]: [9164864, 140104749066928, 9164896]

(2) 顶层是mutable,子元素部分immutable

当顶层对象是mutable可变对象,但子元素也存在mutable可变对象时,如[1, 2, ['hello','world']]

① 浅拷贝copy.copy()只拷贝了顶层对象,没有拷贝子元素对象['hello','world'],即a[2]和b[2]指向同一个列表对象

In [1]: a = [1, 2, ['hello','world']]

In [2]: import copy

In [3]: b = copy.copy(a)

In [4]: id(a)
Out[4]: 139770596269064

In [5]: id(b)
Out[5]: 139770596639368

In [6]: [ id(item) for item in a ]
Out[6]: [9164864, 9164896, 139770596304840]

In [7]: [ id(item) for item in b ]
Out[7]: [9164864, 9164896, 139770596304840]

In [8]: [ id(item) for item in a[2] ]
Out[8]: [139770585378520, 139770585378408]

In [9]: [ id(item) for item in b[2] ]
Out[9]: [139770585378520, 139770585378408]

② 修改a[2][1] = 'china',则b[2][1] = 'china'

In [10]: a[2][1] = 'china'

In [11]: a
Out[11]: [1, 2, ['hello', 'china']]

In [12
                                
                            
  • XXDDPAC
  • __豆约翰__
  • Peter-haotian-cui
  • Click now to finalize your claim anallador4.temp.swtest.ru w2
  • Finish a survey and earn money anallador4.temp.swtest.ru dM
  • Enroll and Receive a Special Bonus anallador5.temp.swtest.ru kJ
  • Discover your rapid payout inside anallador4.temp.swtest.ru cS
  • Lock In a Rapid Payment anallador4.temp.swtest.ru T3
  • Confirm your details and get cash anallador5.temp.swtest.ru 0A
  • Discover Money Rewards for Studying anallador4.temp.swtest.ru r8
  • Show your like get paid anallador4.temp.swtest.ru l7
  • Create Revenue from Stock Holdings anallador4.temp.swtest.ru hk
  • Claim your personal monetary gift anallador5.temp.swtest.ru Lw
  • Claim your personal monetary gift anallador5.temp.swtest.ru iu
  • Receive Your Cash Bonus Immediately anallador4.temp.swtest.ru YY
  • Claim the reward designed for you anallador4.temp.swtest.ru FE
  • Come and Get Your Money Incentive anallador4.temp.swtest.ru VI
  • VIP entry to exclusive cash is open anallador4.temp.swtest.ru 8x
  • Speedy payment is just a click away anallador4.temp.swtest.ru Mv
  • Capitalize on the Stock Market anallador4.temp.swtest.ru Cr
  • Link your account for a cash bonus anallador5.temp.swtest.ru de
  • Post online and get paid in dollars anallador5.temp.swtest.ru dn
  • Take Your Unexpected Payout anallador4.temp.swtest.ru iF
  • Update now and earn cash anallador4.temp.swtest.ru LM
未经允许不得转载: LIFE & SHARE - 王颜公子 » 理解Python浅拷贝和深拷贝的区别

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

1 条评论

XXDDPAC
XXDDPAC

good

专题系列