Python Learning Note 4 (shallow copy, deep copy)

Source: Internet
Author: User
Tags shallow copy

Note: This blog moved from http://www.cnblogs.com/Eva-J/p/5534037.html, the need for friends want you to go to the original blog to watch, give original more respect. Note: I write again is to deepen their impressions, for reference is also convenient, content may be in accordance with their own circumstances have been reduced, so still hope you go to the original blog to watch, thank you ~

Variables of 1.Python and their storage form

In Python learning Note 1, we simply mentioned the concept of variables, so now it's easy to understand how variables store data.

Python stores variables in a way that refers to semantics, storing only the memory address where the value of the variable resides, not the value of the variable itself.

 in Python, a variable holds a reference to an object (value), which we call referential semantics. In this way, the variable requires a consistent amount of storage space because the variable simply holds a reference. Also known as Object semantics and pointer semantics. value Semantics : Some languages do not use this way, they put the value of the variable directly in the memory of the variable, which we call the value semantics, such as the C language, using this storage, each variable in the memory of the space will depend on the actual size of the variable, cannot be fixed. Differences in value semantics and referential semantics: value semantics:   dead, silly,  simple,  concrete, replicable  : Living, intelligent, complex, abstract, non-replicable

Let's take a look at an easy-to-understand diagram that understands the semantics of Python and the storage of C-language values in memory, and about two graphs representing the difference between variable storage in Python and variable storage in C:

2.Python how to store various types of data

Since the variables in Python are all cited, the data structure can contain the underlying data type, which results in the storage of the datastore in Python, where the address of the variable is stored, not the value itself; For complex data structures, The storage inside is just the address of each element.

Each initialization of a variable opens up a new space to assign the address of the new content to the variable. For the most part, we repeat the assignment to the STR1, in fact in memory changes such as:

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

For complex data types, changing their internal values affects variables such as:

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, overwriting the address of the original list, and 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

Figuring out the above, and then discussing the assignment of variables, becomes very simple.

Assignment of A.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.

B. Assigning values in complex data structures

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. Simple analogy, we go out to eat, lst1 and lst2 like a table to eat two people, two people common a desk, as long as the table does not change, the dishes on the table changed two people are common feelings.

4. Copy

We have learned more about the process of assigning variables. For complex data structures, the assignment is equal to the full sharing of the resource, and a change in value is completely shared by another value.

Sometimes, however, we need to keep a copy of the original content of a piece of data, and then process the data, and it's not wise to use assignments at this time. Python provides a copy module for this requirement. Two main copy methods are available, one for normal copy and the other for deepcopy. We call the former a shallow copy, the latter a deep copy.

Deep copy has always been an important point of knowledge for all programming languages, so let's look at the differences from the memory point of view.

Shallow copy

First, let's take a look at the shallow copy. Shallow copy: No matter how complex the data structure, shallow copy will only copy one layer. Let's look at a picture below to see the concept of shallow copy.

Looking at the two graphs above, we added a list of sourcelist,sourcelist = [' str1 ', ' str2 ', ' str3 ', ' str4 ', ' str5 ', [' str1 ', ' str2 ', ' str3 ', ' str4 ', ' STR5 '];

The image on the right is based on the original of a shallow copy of the copylist,copylist = [' str1 ', ' str2 ', ' str3 ', ' str4 ', ' str5 ', [' str1 ', ' str2 ', ' str3 ', ' str4 ', ' STR5 '];

SourceList and Copylist look exactly the same on the surface, but in fact a new list has been generated in memory, copy the Sourcelst, get a new list, store 5 strings and a list of memory addresses.

Let's look at the following actions for two lists, the red box is the variable initialization, initialize the above two lists, we can work on the two lists separately, such as inserting a value, what will we find? As shown below:

From the above code we can see that for the Sourcelst and Copylst list to add an element, the two lists seem to be independent of the same has changed, but when I modify the LST, the two lists have changed, this is why? Let's look at an in-memory change chart.

We can know that the sourcelst and Copylst lists all store a lump of address, when we modify the SourceLst1 element, equivalent to the ' sourcechange ' address replaced the original ' str1 ' address, So the first element of Sourcelst has changed. The Copylst still stores the str1 address, so copylst will not change.

When the Sourcelst list changes, the LST memory address stored in COPYLST does not change, so when LST changes, the Sourcelst and copylst two lists change.

This happens in dictionaries, list sets, dictionary sets, list sets, and nested lists of complex data structures, so when our data types are complex, it's very careful to use copy to make shallow copies ...

Deep copy

Just now we know the meaning of the shallow copy, but when we write the program, we are hoping that the complex data structure between the complete copy and they do not have a penny relationship between them, what should be done?

We introduce a deep copy concept, a deep copy--another deepcopy method provided by the Python copy module. A deep copy completely duplicates all the data associated with the original variable, generating exactly the same set of content in memory, and any modification to one of the two variables in this process will not affect the other variables. Let's try it out here.

Looking at the results of the above implementation, this time we will not be affected by the list of copies, regardless of whether we operate directly on the list or on other data structures nested inside the list. Let's look at the state of these variables in memory:

Looking at the above, we know the principle of deep copy. In fact, deep copy is to re-open a piece of space in memory, no matter how complex the data structure, as long as it encounters a possible change in the type of the information, it re-open a memory space to copy the content, until the last layer, no longer have complex data types, keep its original reference. This way, no matter how complex the data structures are, the changes between them do not affect each other. This is the deep copy ~ ~ ~

Python Learning Note 4 (shallow copy, 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.