There are three types of object copy used in Python: assignment, shallow copy, and deep copy. They are both different and related, just recently encountered this kind of problem, research.

One, the assignment

In python, object assignments are simple object references, unlike C++. As follows:

a = [1.2.3."hello"["python"."C++"]]

b = a

print a==b #True
Copy the code

In this case, B and A are the same, they refer to the same piece of memory, and B is just an alias for A, a reference. We can use whether a and B are the same, return True, they have the same address, the same content.

Assignment operations (including objects as arguments and return values) do not open up new memory; they simply copy the reference to the new object. That is, there is no memory overhead other than the name B.

If you modify A, you affect B; Similarly, changing B affects A.

a = [1.2.3."hello"["python"."C++"]] 
b = a 
b.append("ADD") 
print("a=",a,"b=",b)

#a=[1, 2, 3, 'hello', ['python', 'C++'], 'ADD'] b=[1, 2, 3, 'hello', ['python', 'C++'], 'ADD']
Copy the code

Shallow copy

A shallow copy creates a new object whose contents are references to the original object.

Shallow copies come in three forms: slicing operations, factory functions, and the copy function in the copy module.

For example, for the above A:

1, slice operation: b = a[:] or b = [each for each in a]

B = list(a) b = list(a)

3, copy function: b = copy. Copy (a)

The shallow copy of b is no longer a. Using IS, you can see that they are not the same object. Using ID, you can see that they do not point to the same memory. But when we use id(x) for x in a and id(x) for x in b, we can see that the addresses of the elements are the same.

In this case, A and B are different objects, and modifying B theoretically does not affect A. Such as b.a ppend ([4, 5)).

a = [1.2.3."hello"["python"."C++"]]

b = a[:]

b.append("ADD")

print "a",a,"b",b

#a [1, 2, 3, 'hello', ['python', 'C++']] b [1, 2, 3, 'hello', ['python', 'C++'], 'ADD']
Copy the code

Note, however, that a shallow copy is called a shallow copy because it only copies one layer, and there is a nested list in A. If we modify it, the situation will be different.

A [4]. Append (” C “). If you look at B, you will see that B has also changed. This is because you changed the nested list. Modifying an outer element modifies its references so that they point to another location, modifies elements in a nested list, and the address of the list changes so that they point to the same location.

Deep copy

Deepcopy has only one form, the deepcopy function in the copy module.

In contrast to a shallow copy, a deep copy copies all elements of an object, including multiple layers of nested elements. Therefore, its time and space overhead is high.

Deepcopy (a) = copy. Deepcopy (a) = copy. Even if the nested list has a deeper level, it doesn’t matter because the deep-copied object is a completely new object, no longer associated with the original object.

Warning about copy operations

1. There is no copying of non-container types such as numbers, characters, and other “atomic” types. All that is produced is a reference to the original object.

2. If the tuple variable values contain objects of atomic type, even if a deep copy is used, only a shallow copy can be obtained.