python中的浅拷贝和深拷贝

时间:2021-08-24 22:26:55

本篇介绍下python中的深拷贝和浅拷贝

对于基本类型、类、不可变类型等方面进行介绍

拷贝的本质是复制,需要引入copy模块。

 

先介绍拷贝之前先说下赋值操作:

In [1]: a=10

In [
2]: b=a

In [
3]: a is b
Out[
3]: True

In [
4]: a=[1,2]

In [
5]: b=a

In [
6]: a is b
Out[
6]: True

总结:赋值操作生成的新对象和对象是同一个对象

基本类型:

# 浅拷贝
In [1]: import copy

In [
2]: a=[10,20]

In [
3]: b=[30,40]

In [
4]: c=[a,b]

In [
5]: d=copy.copy(c)

In [
6]: c is d
Out[
6]: False

In [
7]: c
Out[
7]: [[10, 20], [30, 40]]

In [
8]: d
Out[
8]: [[10, 20], [30, 40]]

In [
9]: a.append(50)

In [
10]: c
Out[
10]: [[10, 20, 50], [30, 40]]

In [
11]: d
Out[
11]: [[10, 20, 50], [30, 40]]

In [
12]: c is d
Out[
12]: False

#深拷贝
In [13]: a=[10,20]

In [
14]: b=[10,20]

In [
15]: c=[a,b]

In [
16]: d=copy.deepcopy(c)

In [
17]: c is d
Out[
17]: False

In [
18]: a.append(50)

In [
19]: c
Out[
19]: [[10, 20, 50], [10, 20]]

In [
20]: d
Out[
20]: [[10, 20], [10, 20]]

In [
21]: c is d
Out[
21]: False

总结:浅拷贝即只复制第一层数据,更深层的引用不管,深拷贝,是最深层值得拷贝。

类:

class Person(object):
def __init__(self, name):
self.name
= name


p1
= Person("lingling")
p2
= Person("lingling")

print(p1 is p2) # False
print(p1 == p2) # False

总结:因为p1和p2不是同一个对象,所以返回False,==比较是比较的值,因为这是自定义的类,python解释器并不知道比较的规则,所以使用默认的比较规则,即调用这个对象所属类中的魔法方法

__eq__(),因为Person类并没有重写这个方法,所以默认调用object中的__eq__()方法

object中的__eq__()方法,默认使用了is的比较值。

    def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass

重写__eq__()方法

class Person(object):
def __init__(self, name):
self.name
= name

def __eq__(self, other):
return self.name == other.name


p1
= Person("lingling")
p2
= Person("lingling")

print(p1 is p2) # False
print(p1 == p2) # True

不可变类型:

In [22]: a=(1,2)

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

In [
24]: a is b
Out[
24]: True

In [
25]: b=copy.deepcopy(a)

In [
26]: a is b
Out[
26]: True

In [
27]: a=[10,20]

In [
28]: b=[30,40]

In [
29]: c=(a,b)

In [
30]: d=copy.copy(c)

In [
31]: c is d
Out[
31]: True

In [
32]: d=copy.deepcopy(c)

In [
33]: c is d
Out[
33]: False

总结:对于元组这种不可变类型,是不能修改的,所以Python为了省存储空间,对于不可变类型,内存中始终维护着一份数据。