对象的深浅拷贝

时间:2021-10-18 19:47:31
#!/usr/bin/env python
# -*- coding: utf-8 -*- # author:albert time:2019/4/19 0019 import copy class MobilePhone: def __init__(self,cpu,screen): self.cpu = cpu self.screen = screen class Cpu: pass class Screen: pass c1 = Cpu() s1 = Screen() m1 = MobilePhone(c1,s1) #子对象是相同的 m2 = copy.copy(m1) #子对象是不同de m3 = copy.deepcopy(m1) print(m1,m1.cpu,m1.screen) print(m2,m2.cpu,m2.screen) print(m3,m3.cpu,m3.screen)