Deep copy and shallow copy in Python
This article mainly introduces the deep copy and shallow copy details in Python. This article describes the variables-object-reference, variable object-immutable object, copy and other content. For more information, see
To understand the deep copy in Python, you need to understand the following concepts:
Variable-reference-object (mutable object, immutable object)-slice-copy (Shortest copy, deep copy)
[Variable-object-reference]
In Python, everything is an object, for example, 3, 3.14, 'Hello', [,], {'A': 1 }......
Even the connected type itself is an object, type object
The variables in Python are different from those in C/C ++/Java. It refers to object reference. Python is a dynamic type.
To determine the type of the variable.
Assign values separately: for example:
Copy the Code as follows:
>>> A = 3
After running a = 3, variable a becomes a reference of object 3. Internally, variables are actually a pointer to the object's memory space.
Because Python variables are just object references or pointers to objects, you can often change variable references in programs.
Copy the Code as follows:
>>> X = 42 # bind a variable to an integer object
>>> X = 'hello' # It is now a string
>>> X = [1, 2, 3] # The list is now added.
Professional expressions are as follows:
A variable is an element of a system table and has a space for connecting to objects.
An object is a piece of memory allocated to store the value it represents
A reference is a pointer automatically formed from a variable to an object.
Note: The type is an object, not a variable.
For example, an integer object 3 contains two duplicates, like a = 3.
1. The value is 3.
2. A header information: tells Pthyon that this is an integer object [equivalent to a pointer to an int]
Shared reference: for example:
Copy the Code as follows:
>>> A = 3
>>> B =
After running the value assignment statement B = a, variables a and B point to the memory space of the same object.
As you can see, the IDs of a and B are exactly the same, pointing to the same integer object 3, or the same memory.
If a is deleted
The introduction of the copy concept is aimed at the potential side effects of shared references of mutable objects.
[Mutable object-immutable object]
An unchangeable object in Python is an object that cannot be modified once created, including a string, ancestor, or number.
In Python, variable objects refer to objects that can be modified, including lists and dictionaries.
The a and B mentioned above are all integers, and integers are immutable objects. If they are mutable objects, they are another thing.
Copy the Code as follows:
>>> L1 = [2, 3, 4] # The L1 variable points to a mutable object: List
>>> L2 = L1 # After the L1 value is assigned to L2, the two share the reference to the same list object [1, 2, 4].
>>> L1 [0] = 200 # the value of the first element in L1 is changed because the list is variable.
>>> L1; L2 # after changing, L1 and L2 change at the same time because the object value has changed.
[200, 3, 4]
[200, 3, 4]
If you do not want to change the list L2 value, there are two methods: Slice and copy module.
Copy the Code as follows:
>>> L1 = [2, 3, 4]
>>> L2 = L1
>>> Id (L1); id (L2) # Share and reference a mutable object
45811784L
45811784L
>>> L2 = L1 [:] # Slice operation
>>> Id (L1); id (L2) # after slicing, the object will be different.
45811784L
45806920L
> L1 [0] = 200
>>> L1; L2 # L1 changed, but L2 did not
[200, 3, 4]
[2, 3, 4]
[Copy]
1. The slicing technology is applied to all sequences, including list, string, and ancestor.
>>> However, slice cannot be applied to dictionaries. Only the D. copy () method or D. deepcopy () method can be used for dictionaries.
2. Deep copy, which can be used for sequences or dictionaries
Copy the Code as follows:
>>> Import copy
>>> X = copy. copy (Y) # shallow copy: only top-level objects, or parent objects, are copied.
>>> X = copy. deepcopy (Y) # Deep copy: copy all objects, top-level objects, and nested objects. Or parent-level objects and their sub-objects
If the dictionary contains only top-level objects:
If the dictionary contains nested objects:
[Conclusion]
Both the source object and the source object occupy different memory space.
If the source object only has a level-1 directory, make any changes to the source object without affecting the copy objects in depth.
If the source object has more than one level of directory, any changes made to the source will affect the shortest copy, but will not affect the deep copy.
The slice of the sequence object is actually a shortest copy, that is, only the top-level objects are copied.