拷贝
from copy import copy,deepcopy
class Dog:
def __init__(self,name,color='黑色'):
self.name = name
self.color = color
class Person:
def __init__(self,name,age=0,gender='女'):
self.name = name
self.age = age
self.gender = gender
def __repr__(self):
return f'<{str(self.__dict__)[1:-1]},id:{id(self)}>'
p1 = Person('小明')
print('p1',p1)
p3 = copy(p1)
p4 = deepcopy(p1)
print('p3',p3)
print('p4',p4)
print('=======================================')
p1 = Person('tom',19,'男')
p1.dog = Dog('大黄','黄色')
print(p1)
p2 = copy(p1)
print('p2:',p2)
print('========================================')
p1 = Person('tom',19,'男')
p1.dog = Dog('大黄','黄色')
print(p1)
p2 = deepcopy(p1)
print('p2:',p2)
转载请注明原文地址:https://ipadbbs.8miu.com/read-54095.html