Python learning [shallow copy]

foreword

In the previous article, Python Learning [Inheritance, Encapsulation, Polymorphism] mainly learned the three major characteristics of object-oriented. This article records the learning of shallow copy of python , and the next article will continue to learn deep copy.

insert image description here

easy to understand

shallow copy:Python copies are generally shallow copies, when copying,The contents of sub-objects contained by the object are not copied,therefore,The original object and the copied object will refer to the same subobject(If you don't understand well, you can continue to look at the two pictures below)

shallow copy

We can implement a shallow copy through a list (variable sequence). First, we create a list a, and then print out the id address of the list a and the id addresses of its shallow elements and deep elements:

insert image description here

Make a shallow copy of the list a:

insert image description here
From these two figures, it can be found that when the list a is shallow copied ,The memory address pointed to by list a does not change(That is, the object generated after shallow copy and the original object both point to the same memory address);And whether it is the shallow element or the deep element of the original object list a, they point to the same memory address before and after copying.

Next, we make changes to the shallow and deep elements of the object produced by the shallow copy:

Make changes to the shallow elements of the object produced by the shallow copy
insert image description here
Make changes to the deep elements of the object produced by the shallow copy

insert image description here

Schematic diagrams for list shallow copy and deep copy ( Pythontutor visual code tool ):

insert image description here
Here I refer to a video explanation, I think it is good, and I will share it with you~ Click here to view .

From this, we can make a summary of shallow copy:

Shallow copy only copies shallow elements, and the memory address of deep elements does not changeWhen the shallow elements of the new object generated by the copy are changed, the main elements of the original object are not changed但是对拷贝后产生的对象的深层对象进行改变时,那么原对象的深层元素的地址就会发生变化。

shallow copy of class

By giving an example of shallow copy of the list, it is not difficult to find that,Shallow copy copies the main element of the object, which will generate a main element pointing to the new address;The child elements will not be copied, so it is still the same as the address pointed to by the original object.

Then let's take a look at the use of shallow copy of the class:

# 变量赋值和浅拷贝
class CPU():
    pass

class Disk():
    pass
class Computer():
    # 初始化方法
    def __init__(self,cpu,disk):
        self.cpu=cpu
        self.disk=disk
cpu1=CPU()
cpu2=cpu1
print(cpu1,id(cpu1),type(cpu1))
print(cpu2,id(cpu2),type(cpu2))
'''
<__main__.CPU object at 0x7f883d4d5cd0> 140223120760016
<__main__.CPU object at 0x7f883d4d5cd0> 140223120760016
'''
disk1=Disk()
print('disk1的信息',disk1,id(disk1),type(disk1))
computer1=Computer(cpu1,disk1)
print('computer1的信息',computer1,id(computer1),type(computer1))
# 浅拷贝
import copy
computer2=copy.copy(computer1)
print(computer1,id(computer1),computer1.cpu,computer1.disk)
print(computer2,id(computer2),computer2.cpu,computer2.disk)
'''
浅拷贝中子对象即原类的实例对象的子对象(computer1.cpu  computer1.disk)不拷贝 因此拷贝之后的computer2的参数cpu 和 disk 的内存地址也是指向原来computer1的cpu 和 disk所指向的内存地址的  但是 computer1这个实例对象发生拷贝  也就是产生了一个computer2这个新对象   这个新对象指向的id地址也和原对象的不同
<__main__.Computer object at 0x7f883d4d5d50> <__main__.CPU object at 0x7f883d4d5cd0> <__main__.Disk object at 0x7f883d4d5d10>
<__main__.Computer object at 0x7f883d4d5ed0> <__main__.CPU object at 0x7f883d4d5cd0> <__main__.Disk object at 0x7f883d4d5d10>
'''

insert image description here
Graphical process:
insert image description here

one word per article

If a worker wants to do a good job, he must first sharpen his tools.
If there are any deficiencies, thanks for correcting!

Guess you like

Origin blog.csdn.net/weixin_64122448/article/details/131797243