Summary of Four Realization Methods of Shallow Copy in Python

  • 2021-12-12 05:07:38
  • OfStack

Directory Mode 1: Use slices [:] Mode 2: Use factory functions Mode 3: Use the copy method that comes with the data type Mode 4: copy method using copy module

Mode 1: Use slices [:]

List


#  Shallow copy  [:]
old_list = [1, 2, [3, 4]]
new_list = old_list[:]

old_list.append(5)
old_list[2][0] += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))


#  Output result 
Old list: [1, 2, [100, 4], 5] old list id: 4537660608  old list[0] id: 4537659840
new list: [1, 2, [100, 4]] new list id: 4537711424  new list[0] id: 4537659840

Way 2: Use factory functions

Brief introduction of factory function

Factory function looks like a function, but it is actually a class When called, generates 1 instance of the data type

Factory function of variable object

list() set() dict()

List


old_list = [1, 2, [3, 4]]
new_list = list(old_list)

old_list.append(5)
old_list[2][0] += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))

Set


old_set = {1, 2, 3}
new_set = set(old_set)

old_set.add(4)

print("Old set:", old_set, "old set id:", id(old_set))
print("new set:", new_set, "new set id:", id(new_set))


#  Output result 
Old set: {1, 2, 3, 4} old set id: 4484723648
new set: {1, 2, 3} new set id: 4484723872

Dictionary


old_dict = {"name": " Xiao Ming "}
new_dict = dict(old_dict)

old_dict["second"] = "Python"

print("Old dict:", old_dict, "old dict id:", id(old_dict))
print("new dict:", new_dict, "new dict id:", id(new_dict))


#  Output result 
Old dict: {'name': ' Xiao Ming ', 'second': 'Python'} old dict id: 4514161536
new dict: {'name': ' Xiao Ming '} new dict id: 4515690304

Mode 3: Use the copy method that comes with the data type

List


old_list = [1, 2, [3, 4]]
new_list = old_list.copy()

old_list.append(5)
old_list[2][0] += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))


#  Output result 
Old list: [1, 2, [100, 4], 5] old list id: 4309832000  old list[0] id: 4310372992
new list: [1, 2, [100, 4]] new list id: 4309735296  new list[0] id: 4310372992

Set


old_set = {1, 2, 3}
new_set = old_set.copy()

old_set.add(4)

print("Old set:", old_set, "old set id:", id(old_set))
print("new set:", new_set, "new set id:", id(new_set))


#  Output result 
Old set: {1, 2, 3, 4} old set id: 4309931392
new set: {1, 2, 3} new set id: 4309930944

Dictionary


old_dict = {"name": " Xiao Ming "}
new_dict = old_dict.copy()

old_dict["second"] = "Python"

print("Old dict:", old_dict, "old dict id:", id(old_dict))
print("new dict:", new_dict, "new dict id:", id(new_dict))

 

#  Output result 
Old dict: {'name': ' Xiao Ming ', 'second': 'Python'} old dict id: 4308452288
new dict: {'name': ' Xiao Ming '} new dict id: 4308452224

Source code


    def copy(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of the list. """
        pass

It has been written very clearly. This is a shallow copy

Mode 4: copy method using copy module

List


from copy import copy

old_list = [1, 2, [3, 4]]
new_list = copy(old_list)

old_list.append(5)
old_list[2][0] += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))


#  Output result 
Old list: [1, 2, [100, 4], 5] old list id: 4381013184  old list[0] id: 4381159936
new list: [1, 2, [100, 4]] new list id: 4381012800  new list[0] id: 4381159936

Set


from copy import copy

old_set = {1, 2, 3}
new_set = copy(old_set)

old_set.add(4)

print("Old set:", old_set, "old set id:", id(old_set))
print("new set:", new_set, "new set id:", id(new_set))


#  Output result 
Old set: {1, 2, 3, 4} old set id: 4381115552
new set: {1, 2, 3} new set id: 4381115776

Dictionary


old_list = [1, 2, [3, 4]]
new_list = list(old_list)

old_list.append(5)
old_list[2][0] += 97

print("Old list:", old_list, "old list id:", id(old_list), " old list[0] id:", id(old_list[2]))
print("new list:", new_list, "new list id:", id(new_list), " new list[0] id:", id(new_list[2]))
0

Related articles: