Python copy, shallow copy, deep copy understanding __python

Source: Internet
Author: User
Tags shallow copy

How objects are copied in Python:

(1) Assigned value, (2) shallow copy; (3) deep copy

Assignment operations (including objects as arguments, return values) do not open up new memory space, it simply duplicates the new object's reference;

A shallow copy creates a new object whose contents are references to the original object;

A deep copy copies all the elements of an object, including layers of nested elements.

When you want to completely copy an object, a deep copy is used if the object has a nested structure, otherwise you can use a shallow copy.


Turn from: http://www.jb51.net/article/15714.htm

Example:

Import Copy a = [1, 2, 3, 4, [' A ', ' B ']] #原始对象 B = A #赋值, the reference to the object c = Copy.copy (a) #对象拷贝, shallow copy d = Copy.deepcopy (a) #对象拷贝, Deep copy a.append (5) #修改对象a a[4].append (' C ') #修改对象a中的 [' A ', ' B '] Array object print ' A = ', a print ' B = ', b prin T ' C = ', c print ' d = ', D

Output results:
A = [1, 2, 3, 4, [' A ', ' B ', ' C '], 5]
b = [1, 2, 3, 4, [' A ', ' B ', ' C '], 5]
c = [1, 2, 3, 4, [' A ', ' B ', ' C ']]
D = [1, 2, 3, 4, [' A ', ' B ']]


Instructions for use:

Objects in Python are assigned by reference, and if you need to copy objects, you can use the Copy module in the standard library, or use the copy function with the data type such as Dict.

Copy modules in the standard library, including shallow copies and deep copies:

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


The copy method in the Python data type dict is a shallow copy;

Python data Type list replication, such as List_a = list_b[:] is a shallow copy;

The copy method for array in NumPy is a deep copy


Turn from: http://blog.csdn.net/szchtx/article/details/22958333

Detailed Description:

One, assignment--Simple object reference

In Python, an object's assignment is a simple object reference, which is different from C + +. As follows:

List_a = [1,2,3, "Hello", ["Python", "C + +"]]

List_b = List_a

In this case, List_b and list_a are the same, they point to the same memory, List_b is just a list_a alias, is a reference.

We can use the List_b is list_a to judge, return true, indicating that they have the same address and the same content. You can also use the ID (x) for x in List_a, list_b to view the address of the two list.

assignment operations, including objects as arguments, return values, do not open up new memory space, but simply copy references to new objects . In other words, there is no memory overhead other than the name List_b.

Modified the list_a, it affected the list_b; Similarly, the modification of the list_b affected list_a.


Second, shallow copy (shallow copy)

a shallow copy creates a new object whose contents are references to the original object .

There are three types of shallow copies: Slice operations, factory functions, copy functions in the Copy module

For example, for the above list_a,

Slice operation: List_b = list_a[:] or list_b = [each to each in list_a]

Factory function: List_b = list (list_a)

copy function: List_b = copy.copy (list_a)

The list_b of shallow copies is no longer list_a, and using is to discover that they are not the same object, using IDs to view them and not pointing to the same piece of memory. But when we use ID (x) for x in List_a and ID (x) for x in List_b, we can see that the addresses of the elements they contain are the same.

In this case, list_a and List_b are different objects, and the modification of list_b does not theoretically affect list_a. such as List_b.append ([4,5]).

Note, however, that a shallow copy is called a shallow copy, that it only copies only one layer , there is a nested list in the list_a, and if we modify it, the situation is different.

List_a[4].append ("C"). Looking at the List_b, you will find that List_b has also changed. This is because you have modified the nested list. Modifying the outer element modifies its references, lets them point to another location, modifies the elements in the nested list, addresses the list, and points to the same location for the change.


Third, deep copy (deep copy)

Deep copy has only one form, the Deepcopy function in the copy module.

with a shallow copy, a deep copy copies all the elements of the object, including layers of nested elements . Thus, its time and space overhead are high.

Similarly to list_a, if the use of List_b = Copy.deepcopy (list_a), and then modify the list_b will not affect the list_a. Even if the nested list has a deeper level, it will not have any effect, because the deeply copied object is simply an entirely new object, no longer associated with the original object.


Iv. warnings about copy operations

1. For non-container types, such as numbers, characters, and other "atomic" types, no copy is said. All the references to the original object are generated.

2. If the tuple variable value contains an atomic type object, even if a deep copy is used, only a shallow copy can be obtained.

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.