Python deep copy shallow copy or deep copy shallow copy __python

Source: Internet
Author: User
Tags shallow copy
python deep copy shallow copy or deep copy shallow copy

To put it simply.

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 object and its child objects

Use a simple example to illustrate the following:

>>>import Copy
>>>a = [1, 2, 3, 4, [' A ', ' B ', ' C ']]
>>> B = A
>>> C = copy.copy (a)
>>> d = copy.deepcopy (a)

It's easy to understand: A is a list, the element a[4 in the table is also a list (that is, an internal child object); B is another reference to a list, so a, B is exactly the same and can be proven by ID (a) ==id (b).

Line 4th is a shallow copy, line fifth is a deep copy, and ID (c) and ID (d) can be found to be different and not the same as 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, you can show it by doing the following:

>>> 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 ']]

You can see that a, B is affected by operations 1, 2, C is only affected by Operation 2, D is not affected. A, b the same result is very well understood. Since c is a shallow copy of a, only the parent object is copied, so a child object ([' A ', ' B ', ' C ', ' hello ']) changes will affect C;d is a deep copy, completely unaffected by a

To put it simply.

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 object and its child objects

Use a simple example to illustrate the following:

>>>import Copy
>>>a = [1, 2, 3, 4, [' A ', ' B ', ' C ']]
>>> B = A
>>> C = copy.copy (a)
>>> d = copy.deepcopy (a)

It's easy to understand: A is a list, the element a[4 in the table is also a list (that is, an internal child object); B is another reference to a list, so a, B is exactly the same and can be proven by ID (a) ==id (b).

Line 4th is a shallow copy, line fifth is a deep copy, and ID (c) and ID (d) can be found to be different and not the same as 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, you can show it by doing the following:

>>> 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 ']]

You can see that a, B is affected by operations 1, 2, C is only affected by Operation 2, D is not affected. A, b the same result is very well understood. Since c is a shallow copy of a, only the parent object is copied, so a child object ([' A ', ' B ', ' C ', ' hello ']) changes will affect C;d is a deep copy, completely unaffected by a

To put it simply.

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 object and its child objects

Use a simple example to illustrate the following:

>>>import Copy
>>>a = [1, 2, 3, 4, [' A ', ' B ', ' C ']]
>>> B = A
>>> C = copy.copy (a)
>>> d = copy.deepcopy (a)

It's easy to understand: A is a list, the element a[4 in the table is also a list (that is, an internal child object); B is another reference to a list, so a, B is exactly the same and can be proven by ID (a) ==id (b).

Line 4th is a shallow copy, line fifth is a deep copy, and ID (c) and ID (d) can be found to be different and not the same as 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, you can show it by doing the following:

>>> 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 ']]

You can see that a, B is affected by operations 1, 2, C is only affected by Operation 2, D is not affected. A, b the same result is very well understood. Since c is a shallow copy of a, only the parent object is copied, so a child object ([' A ', ' B ', ' C ', ' hello ']) changes will affect C;d is a deep copy, completely unaffected by a

=========== a shallow copy is a copy of a reference to the original object element, in other words, the object produced by the shallow copy itself is new, but its contents are not new, just a reference to the original object. Here's an example.
>>> 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]


As you can see, a shallow copy produces a new object blist, but Alist's content does refer to alist, so when you change the value of Alist, the value of blist changes as well.

But a bit of special caution is that if the object itself is immutable, then a shallow copy produces two values, see this example:
>>> alist = [1, 2]
>>> blist = alist[:]
>>> blist
[1, 2]
>>> alist
[1, 2]
>>> alist[1]=111
>>> alist
[1, 111]
>>> blist
[1, 2]

Why does the second element of Blist not become 111? Because numbers are immutable types in Python.

Here's a recap of the Python standard type categories:
Soft Type: list, dictionary
Immutable types: Numbers, strings, tuples


Understanding the shallow copy, deep copy of what is naturally very clear.
Python has a module copy,deepcopy function for deep copy, the copy function for shallow copies.

Finally, the object is assigned a deep copy or a shallow copy.
Object assignment is actually a simple object reference
>>> A = 1
>>> ID (a)
135720760
>>> B = A
>>> ID (b)
135720760

A and B are exactly the same thing.

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.