[Py]python deep copy and shallow copy

Source: Internet
Author: User
Tags shallow copy

Python deep copy shallow copy or deep copy shallow copy
Simply put.

    1. Copy.copy a shallow copy copies only the parent object and does not copy the inner child objects of the object.
    2. Copy.deepcopy deep Copy Copy objects and their sub-objects

A simple example illustrates the following:

>>>import copy>>>a = [1, 2, 3, 4, ['a', 'b', 'c']]>>> b = a>>> c = copy.copy(a)>>> d = copy.deepcopy(a)

It is easy to understand: A is a list, the table element A[4] is also a list (that is, an internal sub-object), B is another reference to a list, so a, B is exactly the same, can be proved by ID (a) ==id (b).

The 4th line is a shallow copy, the fifth line is a deep copy, and the ID (c) and ID (d) can be found to be different and not the same as the ID (a):

>>> id(a)19276104>>> id(b)19276104>>> id(c)19113304>>> id(d)19286976

As for how to see the difference between a deep/shallow copy, the following actions can be shown:

>>> a.append(5)    #操作1>>> a[4].append('hello')   #操作2

Then look at the results:

>>> a[1, 2, 0, 4, ['a', 'b', 'c', 'hello'], 5]>>> b[1, 2, 0, 4, ['a', 'b', 'c', 'hello'], 5]>>> c[1, 2, 3, 4, ['a', 'b', 'c', 'hello']]>>> d[1, 2, 3, 4, ['a', 'b', 'c']]

It can be found that a, B is affected by Operation 1, 2, C is only affected by Operation 2, D is unaffected. A, b the same result is very good understanding. Because C is a shallow copy of a, only the parent object is copied, so a sub-object ([' A ', ' B ', ' C ', ' hello ']) will change to affect the c;d is a deep copy, completely unaffected by a

Simply put.

1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象。2. copy.deepcopy 深拷贝 拷贝对象及其子对象

A simple example illustrates the following:

>>>import copy>>>a = [1, 2, 3, 4, ['a', 'b', 'c']]>>> b = a>>> c = copy.copy(a)>>> d = copy.deepcopy(a)

It is easy to understand: A is a list, the table element A[4] is also a list (that is, an internal sub-object), B is another reference to a list, so a, B is exactly the same, can be proved by ID (a) ==id (b).

The 4th line is a shallow copy, the fifth line is a deep copy, and the ID (c) and ID (d) can be found to be different and not the same as the ID (a):

>>> id(a)19276104>>> id(b)19276104>>> id(c)19113304>>> id(d)19286976

As for how to see the difference between a deep/shallow copy, the following actions can be shown:

>>> a.append(5)    #操作1>>> a[4].append('hello')   #操作2

Then look at the results:

>>> a[1, 2, 0, 4, ['a', 'b', 'c', 'hello'], 5]>>> b[1, 2, 0, 4, ['a', 'b', 'c', 'hello'], 5]>>> c[1, 2, 3, 4, ['a', 'b', 'c', 'hello']]>>> d[1, 2, 3, 4, ['a', 'b', 'c']]

It can be found that a, B is affected by Operation 1, 2, C is only affected by Operation 2, D is unaffected. A, b the same result is very good understanding. Because C is a shallow copy of a, only the parent object is copied, so a sub-object ([' A ', ' B ', ' C ', ' hello ']) will change to affect the c;d is a deep copy, completely unaffected by a

Simply put.

    1. Copy.copy a shallow copy copies only the parent object and does not copy the inner child objects of the object.
    2. Copy.deepcopy deep Copy Copy objects and their sub-objects

A simple example illustrates the following:

>>>import copy>>>a = [1, 2, 3, 4, ['a', 'b', 'c']]>>> b = a>>> c = copy.copy(a)>>> d = copy.deepcopy(a)

It is easy to understand: A is a list, the table element A[4] is also a list (that is, an internal sub-object), B is another reference to a list, so a, B is exactly the same, can be proved by ID (a) ==id (b).

The 4th line is a shallow copy, the fifth line is a deep copy, and the ID (c) and ID (d) can be found to be different and not the same as the ID (a):

ID (a)
19276104
ID (b)
19276104
ID (c)
19113304
ID (d)
19286976

至于如何看深/浅拷贝的区别,可以通过下面的操作来展现:

A.append (5) #操作1
A[4].append (' Hello ') #操作2

这时再查看结果:

A
[1, 2, 0, 4, [' A ', ' B ', ' C ', ' Hello '], 5]
B
[1, 2, 0, 4, [' A ', ' B ', ' C ', ' Hello '], 5]
C
[1, 2, 3, 4, [' A ', ' B ', ' C ', ' hello ']
D
[1, 2, 3, 4, [' A ', ' B ', ' C ']]

可以发现a、b受了操作1、2的影响,c只受操作2影响,d不受影响。a、b结果相同很好理解。由于c是a的浅拷贝,只拷贝了父对象,因此a的子对象( ['a', 'b', 'c', 'hello'])改变时会影响到c;d是深拷贝,完全不受a的影响? ===========浅拷贝是指拷贝的只是原对象元素的引用,换句话说,浅拷贝产生的对象本身是新的,但是它的内容不是新的,只是对原对象的一个引用。这里有个例子

Alist=[[1, 2], 3, 4]
Blist = alist[:] #利用切片完成一次浅拷贝
ID (alist)
3084416588L
ID (blist)
3084418156L
Alist[0][0] = 5
Alist
[[5, 2], 3, 4]
Blist
[[5, 2], 3, 4]

可以看到,浅拷贝生产了一个新的对象bList,但是aList的内容确实对aList的引用,所以但改变aList中值的时候,bList的值也跟着变化了。但是有点需要特别提醒的,如果对象本身是不可变的,那么浅拷贝时也会产生两个值,见这个例子:

Alist = [1, 2]
Blist = alist[:]
Blist
[1, 2]
Alist
[1, 2]
alist[1]=111
Alist
[1, 111]
Blist
[1, 2]

为什么bList的第二个元素没有变成111呢?因为数字在python中是不可变类型!!这个顺便回顾下Python标准类型的分类:可变类型: 列表,字典不可变类型:数字,字符串,元组理解了浅拷贝,深拷贝是什么自然就很清楚了。python中有一个模块copy,deepcopy函数用于深拷贝,copy函数用于浅拷贝。最后,对象的赋值是深拷贝还是浅拷贝?对象赋值实际上是简单的对象引用

A = 1
ID (a)
135720760
b = A
ID (b)
135720760
```
A and B are exactly the same thing.

Reference: Python's depth copy

[Py]python deep copy and shallow copy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.