Questions about assignment, shallow copy, and deep copy in Python

Source: Internet
Author: User
Tags shallow copy

There are three types of use of object replication in Python, assignment, light copy, and deep copy. They have both a difference and a connection, just recently encountered this kind of problem, under study.

First, assign a value

In Python, the assignment of an object is a simple object reference, which differs from C + +. As follows:

List_a = [All-in-a-box, "Hello", ["Python", "C + +"]]

List_b = List_a

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

We can use List_b is list_a to determine that returning true indicates 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 see the address of the two list.

an assignment operation (including an object as a parameter, a return value) does not open up new memory space, it simply copies a reference to the new object . 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 list_b affected the list_a.

Second, shallow copy (shallow copy)

A shallow copy creates a new object whose content is a reference to the original object .

There are three types of shallow copy: Slice operation, factory function, copy function in copy module

For example, for the above list_a,

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

Factory function: List_b = list (list_a)

copy function: List_b = copy.copy (list_a)

The list_b generated by a shallow copy is no longer list_a, and using is can find that they are not the same object, use the ID to see them, and find that they do not point to the same piece of memory. But when we use the ID (x) for x in list_a and the ID (x) for x in List_b, you can see that the addresses of the elements contained in the two are the same.

In this case, list_a and List_b are different objects, and modifying list_b theoretically does not 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 one layer , and that there is a nested list in list_a, and if we modify it, the situation is different.

List_a[4].append ("C"). Check out List_b and you'll find List_b has changed as well. This is because you modified the nested list. Modifying the outer element modifies its reference to point to a different location, modifies the elements in the nested list, and the address of the list is changed, pointing to the same location.

Third, deep copy

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

corresponding to a shallow copy, a deep copy copies all the elements of the object, including multiple layers of nested elements . As a result, its time and space costs are high.

Similarly to list_a, if you use List_b = Copy.deepcopy (list_a), then modifying list_b will not affect list_a. Even if the nested list has a deeper level, it does not have any effect, because the deep-copied object is simply a completely new object that is 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. The result is a reference to the original object.

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.

Questions about assignment, shallow copy, and deep 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: 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.