Python Foundation shallow copy and deep copy

Source: Internet
Author: User

One, variable assignment:
1. Initialization of variables

Each initialization of a variable opens up a new space to assign the address of the new content to the variable (that is, the variable's memory address changes). For the words, we repeat the assignment to the STR1, in fact the memory changes as shown in the right:

As we can see, str1 in the repetitive initialization process because the address of the element stored in STR1 is changed from the address of ' Hello World ' to ' new Hello World '.

2, the list of variables and additions to change

When you make some additions and deletions to the elements in the list, it does not affect the Lst1 list itself for the entire list address, only changes the address references of its inner elements. But when we re-initialize (assign) a list, we give lst1 this variable an address again,

Overwrite the address of the original list, this time, the memory ID of the Lst1 list has changed. The above principle is the same for all complex data types.

3. Assigning values to variables

(1) the assignment of Str

We have just learned that str1 initialization (Assignment) will result in a change in memory address, from which we can see that the assigned str2 from the memory address to the value are not affected after the STR1 has been modified.

Looking at the changes in memory, the starting assignment allows both the STR1 and STR2 variables to store the ' hello ' World ' address, re-initializes the str1, so that the address stored in STR1 has changed, pointing to the new value, the memory address of the STR2 variable storage is not changed, so it is not affected.

(2) Assignment of the list

The added modification to the list does not change the memory address of the list, Lst1 and lst2 have changed.

Comparing memory graphs It is not difficult to see that when a new value is added to the list, the address of a new element is stored in the list, and the address of the list itself does not change, so the IDs of both Lst1 and lst2 are unchanged and a new element is added.

 Summary: regardless of the complex structure of the list, or the structure of the string, as long as the variable is initialized, it will open up a new storage space (the list of additions and deletions will not open up new memory space). This assigns the value of the variable str2, or the first point of the memory space str1 (does not point to the second initialization of the variable value, that is, the new str1)

Second, copy copy

Shallow copy: No matter how complex the data structure, shallow copy will only copy one layer

>>> Import Copy
>>> list1=[1,2,3,[1,2,3]]
>>> list2=copy.copy (List1)
>>> Print (List1)
[1, 2, 3, [1, 2, 3]]
>>> Print (LIST2)
[1, 2, 3, [1, 2, 3]]
>>> List1.append (4)
>>> list1[3]=[1,2]
>>> Print (List1)
[1, 2, 3, [1, 2], 4] # The outer layer has been copied, opening up new space, the modification of the original data will not affect the new data
>>> Print (LIST2)
[1, 2, 3, [1, 2, 3]] # The inner layer does not replicate, and list1[3]=[1,2] opens up new space, so here List2 still point to the original space [1, 2, 3]
>>>

Deep copy:

Python Foundation shallow copy and deep copy

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.