Python assignment and depth copy

Source: Internet
Author: User
Tags first string shallow copy

The data model talking about object ID

In Python, all data is an object, and the object is a Python abstraction of the data. Each object has its identity,type,value. Once an object is created, its ID does not change, and you can treat it as an object's address in memory. The IS operator compares the IDs of the two objects that are the same, and the ID () function returns an integer representation of the ID.

Object that type

The type of the object determines the operations that the object supports, as well as the possible values for that object. The type () function can get the types of objects. As with an object's ID, the type of the object cannot be changed.

Object that value

The value of the object can be changed. The value of a mutable object is mutable, and the value of an immutable object cannot be changed. Object Variability (immutability) is determined by its type, such as numeric types, string types, and tuple types, which are immutable types, while dictionary and list types are mutable.
When immutable container objects contain mutable objects, their values are variable. But the entire container object is immutable. For example, when a tuple object is nested in a list object, when the list object changes, the entire tuple object remains in the immutable object.

Assignment mechanism
  1. Simple Data type assignment:

    Str1= ' Hello World 'str2=Str1Print(' str1: ' +Str1+ '; ID: ' + Str(ID(STR1)))Print(' str2: ' +str2+ '; ID: ' + Str(ID(STR2)))Print('-' *  *) str1= ' Hi World 'Print(' str1: ' +Str1+ '; ID: ' + Str(ID(STR1)))Print(' str2: ' +str2+ '; ID: ' + Str(ID(STR2)))

    First, create a string object with the variable str1 pointing to ' Hello World '. STR2 = Str1 's role is to make str2 also point to ' Hello World '. STR1 and str2 Save the same memory address.
    After creating the new string object ' Hi World ' and changing the str1 to point to the new string object. The output shows that the ID of the STR2 has not changed, indicating that STR2 still points to the first string object.
  2. List type assignment:

    Lst1=[1,2,3,4,5]lst2=Lst1Print(' Lst1: ' + Str(LST1)+ '; ID: ' + Str(ID(LST1)))Print(' Lst2: ' + Str(LST2)+ '; ID: ' + Str(ID(LST2)))Print(' * ' *  +) Lst1.append (6)Print(' Lst1: ' + Str(LST1)+ '; ID: ' + Str(ID(LST1)))Print(' Lst2: ' + Str(LST2)+ '; ID: ' + Str(ID(LST2))) Lst2.pop ()Print(' Lst1: ' + Str(LST1)+ '; ID: ' + Str(ID(LST1)))Print(' Lst2: ' + Str(LST2)+ '; ID: ' + Str(ID(LST2)))

    First, create a list object Lst1, and let both Lst2 and Lst1 point to the same list. The following append operations and pop operations, whether for Lst1 or lst2, are in fact the same list. Two variables all point to the same list, the action on either variable will affect the other.

Summary, whether it is a simple or complex data type, the assignment action points multiple variables to a block of data. If one of the variables is re-assigned, the other variable still points to the previous data block. For complex data types, a simple append operation does not change the address of the block, nor does it change the actual direction of the variable. Therefore, the operation of one variable is actually an operation on the entire block of data, which in turn affects other variables.
Assignment statements in Python do not copy objects, they create bindings between the target and the object.

About copy

Requirements: Sometimes what we need is a complete copy of the new data, and the changes to the original data will not affect the data that the new variable points to. At this point, Pyhton introduces the concept of a copy.
For a mutable item or a collection that contains a mutable item, you sometimes need a copy so that you can change one copy without changing the other copy. Python provides a dedicated module copy for this requirement. Includes a deep copy, i.e. deepcopy and copy. The difference between a shallow copy and a deep copy exists only in a composite object (the object contains other objects, such as a list or class instance).

  1. Shallow copy
    Shallow copy constructs a new compound object, and then (as far as possible) inserts the reference into the original object.

    ImportCopysource_lst=[' str1 ',' str2 ',' STR3 ', [' STR4 ',' STR5 ',' STR6 ']]copy_lst=Copy.copy (SOURCE_LST)Print(SOURCE_LST)Print(COPY_LST)Print(' * ' *  -) Source_lst.append (' Append str ')Print(SOURCE_LST)Print(COPY_LST)Print(' * ' *  -) source_lst[0]= ' modified str1 'Print(SOURCE_LST)Print(COPY_LST)Print(' * ' *  -) source_lst[3][0]= ' modified STR4 'Print(SOURCE_LST)Print(COPY_LST)Print(' * ' *  -)

    The output from the above code shows that the simple copy of the copy module, the addition of ordinary data types to source_lst, modification and other operations, will not cause copy_lst changes. However, the modification of nested lists causes both Source_lst and copy_lst to change, because a shallow copy only copies the entire reference of the nested list and does not reopen new data space.
  2. Deep copy
    Deep copy constructs a new composite object and then recursively inserts the copy into the object found in the original object.

    ImportCopysource_lst=[' str1 ',' str2 ',' STR3 ', [' STR4 ',' STR5 ',' STR6 ']]deepcopy_lst=Copy.deepcopy (SOURCE_LST)Print(SOURCE_LST)Print(DEEPCOPY_LST)Print(' * ' *  -) Source_lst.append (' Append str ')Print(SOURCE_LST)Print(DEEPCOPY_LST)Print(' * ' *  -) source_lst[0]= ' modified str1 'Print(SOURCE_LST)Print(DEEPCOPY_LST)Print(' * ' *  -) source_lst[3][0]= ' modified STR4 'Print(SOURCE_LST)Print(DEEPCOPY_LST)Print(' * ' *  -)

    Change Copy.copy (Source_lst) to Copy.deepcopy (Source_lst) only, and the others remain unchanged. The addition and modification of SOURCE_LST to ordinary data types will not cause deepcopy_lst changes. For the source_lst nested list modification, Deepcopy_lst also did not change, because of the deep copy, in a recursive way, copy all data types, re-open the data space.

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