Deep copy and shallow copy in Python

Source: Internet
Author: User
Tags shallow copy

Reprint: http://blog.csdn.net/vicken520/article/details/8227524

This problem is often encountered in Java. Time is why you don't write Java.

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 sub-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 a shallow copy copies only the parent object and does not copy the inner sub-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 a shallow copy copies only the parent object and does not copy the inner sub-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

?

===========

A shallow copy is simply a reference to the original object element, in other words, the object 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 the alist, so the blist value changes when the Alist value is changed.

But a bit of a special reminder, if the object itself is immutable, then the shallow copy will also produce 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 the numbers are immutable types in Python!!

This incidentally reviews the classification of Python standard types:
mutable type: list, dictionary
Immutable types: Numbers, strings, tuples


Understand the shallow copy, deep copy is what nature is very clear.
Python has a module copy,deepcopy function for deep copy, copy function for shallow copy.

Finally, is the object assigned to a deep or 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.

Deep copy and shallow copy in Python

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.