Python Assignment and copy (do you really understand)

Source: Internet
Author: User
Tags shallow copy

Phenomenon: First a section of code.

>>>ImportCopy>>> a = [1,2,3,4,['a','b']]>>> B =a>>> C =Copy.copy (a)>>> d =Copy.deepcopy (a)>>> A.append (5)>>>Print(a) [1, 2, 3, 4, ['a','b'], 5]>>>Print(b) [1, 2, 3, 4, ['a','b'], 5]>>>Print(c) [1, 2, 3, 4, ['a','b']]>>>Print(d) [1, 2, 3, 4, ['a','b']]>>> A[4].append ('C')>>>Print(a) [1, 2, 3, 4, ['a','b','C'], 5]>>>Print(b) [1, 2, 3, 4, ['a','b','C'], 5]>>>Print(c) [1, 2, 3, 4, ['a','b','C']]>>>Print(d) [1, 2, 3, 4, ['a','b']]
##### #内存地址 ########

>>> ID (a)
44350024
>>> ID (b)
44350024
>>> ID (c)
44410440
>>> ID (d)
44410760

First, the concept (principle)

1. Before we learn more about the assignment, copy, and deepcopy in Python, it's time to take a moment to learn about the storage of variables in Python memory.

In a high-level language, a variable is an abstraction of memory and its address. For Python, all of Python's variables are objects, variables are stored, and reference semantics are used to store only the memory address where the value of a variable resides, not the variable itself.

2. Assign 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.

3. 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.

4. 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.

Ii. warnings about the copy

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.

Python Assignment and copy (do you really understand)

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.