Python depth copy

Source: Internet
Author: User

I. Introduction of Concepts

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.

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.

Since the variables in Python are all cited, the data structure can contain the underlying data type, causing the address of the variable to be stored in each variable in Python, rather than the value itself;

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:

For complex data structures, the storage of the inside is only the address of each element, and the following gives the storage changes of the underlying type and the data structure type variable re-assignment:

Reference: http://www.cnblogs.com/Eva-J/p/5534037.html

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

If you have a data type that can use other data types as your own elements, then we call that data type 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.

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.

1My_str ="ways"2My_list = [0,1,2,["a","b","C"]]3My_dic = {"name":"ways"," Age": 16}4 Print(ID (my_str))5 Print(ID (my_list))6 Print(ID (my_list[0]), ID (my_list[1]), ID (my_list[2]), ID (my_list[3]))7 Print(ID (my_dic), ID (my_dic["name"]), ID (my_dic[" Age"]))

Output:

2131810169664
2131811101448
1837680272 1837680304 1837680336 2131810205832
2131810159136 2131810169664 1837680784

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

1 A = 12342print(ID (a))3 a = 4564print(ID (a))  5 output:6 19479236810407 1947919757008

Each initialization of a variable opens up a new space to assign the address of the new content to the variable.

2. Impact of changes in data structure internal elements on Python semantic references

1>>> my_list = [1,2,3,4]2>>>Print(ID (my_list))322508870094804>>> My_list.append ("Add")5>>>Print(ID (my_list))622508870094807>>>My_list.pop ()8 'Add'9>>>Print(ID (my_list))Ten2250887009480 One>>>Print(ID (my_list[0])) A1854391984 ->>> my_list[0]="ways" ->>>Print(ID (my_list[0])) the2250887017728 ->>> my_list = [3333,44444,55555] ->>>Print(ID (my_list)) -2250887008776

When you make some additions and deletions to the elements in the list, it does not affect the My_list 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 my_list this variable an address, overwriting the address of the original list, and this time the memory ID of the My_list list has changed.

Third, view variable memory address

>>> a = 1>>> print (ID (a)) >>>1473627824
Four, shallow copy

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

1 ImportCopy2Source_date = [1,2,3,4,5,[10,11,12]]3Copy_date =copy.copy (source_date)4 Print("Source:", ID (source_date))5 Print("copy_date", ID (copy_date))6 Output:7source:20627400014808Copy_date 2062740002632
1Source_date.append ("sourcedate")2Copy_date.append ("copydate")3 Print("Source:", Source_date)4 Print("copy_date", Copy_date)5Copy_date[5][0] =66666 Print("Source:", Source_date)7 Print("copy_date", Copy_date)8Source_date[5][0] = ["ways"]9 Print("Source:", Source_date)Ten Print("copy_date", Copy_date) OneSOURCE_DATE[5] = ["ways","Alex"] A Print("Source:", Source_date) - Print("copy_date", Copy_date) - Output: theSource: [1, 2, 3, 4, 5, [10, 11, 12],'sourcedate'] -copy_date [1, 2, 3, 4, 5, [10, 11, 12],'copydate'] -Source: [1, 2, 3, 4, 5, [6666, 11, 12],'sourcedate'] -copy_date [1, 2, 3, 4, 5, [6666, 11, 12],'copydate'] +Source: [1, 2, 3, 4, 5, [['ways'], 11, 12],'sourcedate'] -copy_date [1, 2, 3, 4, 5, [['ways'], 11, 12],'copydate'] +Source: [1, 2, 3, 4, 5, ['ways','Alex'],'sourcedate'] Acopy_date [1, 2, 3, 4, 5, [['ways'], 11, 12],'copydate']
Five, deep copy

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.

1SOURCE_STR = ["ways","Tanks","Yang", [A]]2Copy_str =copy.deepcopy (SOURCE_STR)3 Print(ID (SOURCE_STR), ID (source_str[0]), ID (source_str[3]), ID (source_str[3][0]))4 Print(ID (COPY_STR), ID (copy_str[0]), ID (copy_str[3]), ID (copy_str[3][0]))5SOURCE_STR[3][1] = 66666 Print(ID (SOURCE_STR), ID (source_str[0]), ID (source_str[3]), ID (source_str[3][1]))7 Print(ID (COPY_STR), ID (copy_str[0]), ID (copy_str[3]), ID (copy_str[3][1]))8 Output:92354768160392 2354767917832 2354768160584 1833617072Ten2354768160328 2354767917832 2354768160200 1833617072 One2354768160392 2354767917832 2354768160584 2354733559504 A2354768160328 2354767917832 2354768160200 1833617104
Vi. Summary
    • The assignment of an object in Python is an object reference (memory address) Pass
    • With Copy.copy (), you can make a shallow copy of the object, which duplicates the object, but still uses the original reference for the element in the object.
    • If you need to copy a container object and all of its elements (containing the element's child elements), you can use Copy.deepcopy () to make a deep copy

Python depth 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.