python learning [deep copy]

#My Programming Language Learning Notes#

Preface

The previous article Python Learning [Shallow Copy] learned about shallow copy related content in python. This article continues to learn about deep copy.

Brief review

Shallow copy only copies shallow elements, and the memory address of deep elements does not change.; 当对拷贝产生的新的对象的浅层元素进行更改时,原对象的主元素不做改变; But when the deep object of the object generated after copying is changed, the address of the deep element of the original object will change.

Insert image description here

而python中的深拷贝(deepcopy) 不仅会拷贝对象的主层元素,深层元素也会拷贝。

Example

We also give an example of deep copying a list first. Note that when performing deep copy, we need to introduce a copy template block first:

#深层拷贝需要引入一个 拷贝python copy模板块
import copy

Then use the list to verify the deep copy:

import copy

a=[11,22,33,[1,2,3,[4,5]]]
d=copy.deepcopy(a)
print('--------------a的相关信息-------------')
print(a,id(a))   #[11, 22, 33, [1, 2, 3, [4, 5]]] 2196544302400
print('------------a中元素的相关信息--------------')
print(a[0],id(a[0]),a[1],id(a[1]),a[2],id(a[2]))
#11 140721422918760 22 140721422919112 33 140721422919464
a[3][1],id(a[3][1]))
# 2 140721422918472
print(a[3][3][1],id(a[3][3][1]))
# 5 140721422918568


print('-------------d的相关信息--------------------')
print(d,id(d))   #[11, 22, 33, [1, 2, 3, [4, 5]]] 2196544300928
print('-------------d中元素的相关信息----------------')
print(d[0],id(d[0]),d[1],id(d[1]),d[2],id(d[2]))

# 11 140721422918760 22 140721422919112 33 140721422919464

Because of 列表是可变序列this, we cannot see the relationship diagram of the memory address from the id address; but we can view the change of the memory by changing the value of the object d obtained after copying:

print('-----------------改变深拷贝后的浅层元素和深层元素-----------------')
d[2]=100
print('改变拷贝后的对象的浅层元素后的a的相关信息:',a[2],id(a[2]))
#改变拷贝后的对象的浅层元素后的a的相关信息: 33 140721422919464

d[3][1]=999
d[3][3][1]=888
print('改变拷贝后的对象d的深层元素后的a的相关信息:',a[3][1],id(a[3][1]))
# 改变拷贝后的对象d的深层元素后的a的相关信息: 2 140721422918472
print(a[3][3][1],id(a[3][3][1]))
# 5 140721422918568

Through the running results, we can see that the id address of the original object a will not change no matter whether the shallow element or the deep element of d is changed, which means,Deep copy deepcopy will copy both the deep object and the surface object of the object to form a new object.

So its memory graph looks like this:
Insert image description here

deep copy of class

# python中的深拷贝
class CPU():
    pass
class Disk():
    pass
class Computer():
    def __init__(self,cpu,disk):
        self.cpu=cpu
        self.disk=disk
cpu1=CPU()
disk1=Disk()
print('cpu1的信息:',cpu1,id(cpu1))
print('disk1的信息:',disk1,id(disk1))
'''
cpu1的信息: <__main__.CPU object at 0x7f7b6e4fca90> 140168108427920
disk1的信息: <__main__.Disk object at 0x7f7b6e4fcb10> 140168108428048
'''
# 不要忘记传参
computer1=Computer(cpu1,disk1)
# 传入拷贝模板
import copy
computer3=copy.deepcopy(computer1)
print('computer1的相关信息',computer1,id(computer1),computer1.cpu,computer1.disk)
print('---------------深拷贝后的对象的信息——computer3-------------\n')
print('computer3的相关信息',computer3,id(computer3),computer3.cpu,computer3.disk)
'''
computer1的相关信息 <__main__.Computer object at 0x7f92ae4b9bd0> 140267966143440 <__main__.CPU object at 0x7f92ae4b9ad0> <__main__.Disk object at 0x7f92ae4b9b50>
computer3的相关信息 <__main__.Computer object at 0x7f92ae4b9c90> 140267966143632 <__main__.CPU object at 0x7f92ae4bd890> <__main__.Disk object at 0x7f92ae468c50>
'''

Similarly, we draw the corresponding memory graph:
Insert image description here

One sentence per article

Forewarned is forearmed, without prejudging the waste!

If there are any deficiencies, thank you for correcting me!

おすすめ

転載: blog.csdn.net/weixin_64122448/article/details/132702043
おすすめ