Analysis of deep copy and shallow copy (slicing of python and go)

Table of contents

1. Concept analysis

1.1 Shallow copy

1.2 Deep copy

2. Slicing of python and go

2.1 python slicing

2.2 Go Slicing


1. Concept analysis

Shallow Copy and Deep Copy are two concepts about object or data structure replication.

1.1 Shallow copy

Shallow copy means that when copying one object to another object, only the reference of the object is copied but not its content. This means that the copied object shares the same memory space as the original object, and modifications to one will affect the other . Shallow copy is generally suitable for simple data types, such as variables of basic types, arrays, etc.

1.2 Deep copy

A deep copy is when copying one object to another, copying the contents of the object and its references. This means that the copied object is completely independent from the original object, and modifications to one object will not affect the other . Deep copy is generally suitable for complex data types, such as objects, nested data structures, etc.

2. Slicing of python and go

2.1 python slicing

The python slicing operation only copies the reference of the original list, not the actual elements. The slicing operation is a shallow copy (Shallow Copy) rather than a deep copy (Deep Copy).

When slicing a list, a new list object is created, but that object shares the same element objects as the original list. This means that modifying elements of one list will have an effect on the other list.

Here is an example to illustrate the shallow copy nature of the slice operation:

python
original_list = [1, 2, 3, [4, 5]]
sliced_list = original_list[:]

# 修改切片列表中的元素
sliced_list[0] = 6

# 修改切片列表中的子列表元素
sliced_list[3][0] = 7

print(original_list)  # 输出: [1, 2, 3, [7, 5]]
print(sliced_list)    # 输出: [6, 2, 3, [7, 5]]

In this example, we create an original list with sublists original_list. [:]Then, we created a list of slices using the slice operator sliced_list. When we modify sliced_listelements in a sliced ​​list, the original list original_listremains unchanged. However, when we modify sublist elements in a sliced ​​list, the original list is also affected. This is because the slicing operation only copies the references of the original list, not the actual element objects. So, modifying a mutable object in a slice (such as a sublist) affects the original list

If you need to do a deep copy operation, you can use copythe module's deepcopy()function to create a completely separate copy.

2.2 Go Slicing

In Go, the copy operation of a slice is a shallow copy (Shallow Copy) instead of a deep copy (Deep Copy).

When you assign a slice to another slice, you actually create a new slice object, but that object shares the underlying array with the original slice. This means that modifying elements of one slice will have an effect on the other slice.

Here is an example to illustrate the shallow copy feature of go slices:

originalSlice := []int{1, 2, 3, 4, 5}
copiedSlice := originalSlice

// 修改切片中的元素
copiedSlice[0] = 6

fmt.Println(originalSlice) // 输出: [6 2 3 4 5]
fmt.Println(copiedSlice)   // 输出: [6 2 3 4 5]

The function in Go copy()is used to copy one slice to another, ensuring that the two slice objects are independent of each other .

Guess you like

Origin blog.csdn.net/weixin_45440484/article/details/131740125