(Ultimate modified Version) Python Foundation--Assignment and depth copy

Source: Internet
Author: User
Tags shallow copy

Novice programming partners will have some doubts about the use of deep-level copies, today we will combine the characteristics of the Python variable storage from the memory point of view to talk about assignment and depth copy ~ ~ ~

The variable and its storage of the--python of preliminary knowledge

Before we get into the details of Python assignment, copy, and Deepcopy, 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 .

Referential semantics: 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
An explanation of semantic and value semantics of citing terms

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:

  

Preliminary knowledge two-address storage and change of the basic data types

Data types in Python include: bool, int, long, float, str, set, list, tuple, dict, and so on. We can roughly classify these data types as simple data types and complex data Structures .

My criteria for partitioning is that if you have a data type that can use other data types as your own elements, I think it's a structure. There are many types of data structures, but only the collection, sequence, and mapping of three structures are commonly used in Python. Corresponding to the set in Python, list (tuple, str), dict, commonly used data types are int, long, float, bool, str and other types. (The str type is special, because from the C language point of view, Str is actually a set of char, but this is not related to this article, so we do not discuss this issue)
data structure and its division basis

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

  

  1. The effect of data type reinitialization on Python semantic references

  each initialization of a variable opens up a new space to assign the address of the new content to the variable . 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. Impact of changes in data structure internal elements on Python semantic references

  For complex data types, the effect of changing its internal values on variables is:

  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.

assigning values to variables

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

  Assignment of 1.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. Assigning values in complex data structures

We've just looked at the assignment of simple data types and now look at the impact of complex data structure changes on memory.

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.

Initial Knowledge 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 that the left image represents 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 graph:

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 ~ ~

(Turn) (Ultimate modified Version) Python Foundation--Assignment and depth copy

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.