Python assignment and copy (Do you know), python assignment

Source: Internet
Author: User

Python assignment and copy (Do you know), python assignment

Symptom: start with the previous code.

>>> Import copy >>> a = [1, 3, 4, ['A', 'B'] >>> B = a >>> c = copy. copy (a) >>> d = copy. deepcopy (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']
###### Memory address ########

>>> Id ()
44350024
>>> Id (B)
44350024
>>> Id (c)
44410440
>>> Id (d)
44410760

I. concepts (Principles)

1. Before learning about values, copies, and deepcopy in python in detail, we still need to take a moment to learn about the storage of variables in python memory.

In advanced languages, variables are the abstraction of memory and Its addresses. For python, all the variables in python are objects. The storage of variables adopts the reference semantic method, and only stores the memory address where the value of a variable is located, instead of the variable itself.

2. Assignment

In python, object 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 piece of memory. list_ B is just the alias of list_a and is a reference.

We can use list_ B is list_a to determine whether to return true, indicating that they have the same address and content. You can also use id (x) for x in list_a and list_ B to view the addresses of two lists.

The value assignment operation (including the object as a parameter and return value) does not open up new memory space. It just copies the reference of the new object.. That is to say, there is no memory overhead except the name list_ B.

List_ B is affected when list_a is modified. Similarly, list_a is affected when list_ B is modified.

3. Shallow copy

A new object will be created for the shortest copy, and its content is the reference of the original object..

There are three methods for copying: slicing, factory functions, and copy functions in the copy module.

For example,

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 the shallow copy is no longer list_a. It can be found that they are not the same object by using the id to view and that they do not point to the same piece of memory. However, 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 are the same.

In this case, list_a and list_ B are different objects. Modifying list_ B theoretically does not affect list_a. For example, list_ B .append ([4, 5]).

Note that,The reason why a copy is called a copy is that it only copies one layer.In list_a, there is a nested list. If we modify it, the situation will be different.

List_a [4]. append ("C "). Check list_ B and you will find that list_ B has also changed. This is because you modified the nested list. Modifying the outer element will modify its reference to point them to another location, modify the element in the nested list, and the address of the list will change, pointing to the same location.

4. Deep copy

Deep copy is only in the form of the deepcopy function in the copy module.

Corresponds to a shortest copy. It copies all the elements of an object, including multiple layers of nested elements.. Therefore, it requires a high cost of time and space.

Similarly, if list_ B = copy. deepcopy (list_a) is used for list_a, modifying list_ B will not affect list_a. Even if the nested list has a deeper level, it will not have any impact, because the object copied in depth is a completely new object and will not be associated with the original object.

Ii. Copy warning

1. For non-container types, such as numbers, characters, and other "Atomic" types, do not copy them. All generated are references of the original object.

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

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.