拷贝

    技术2025-01-07  8

    拷贝

    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) # 1.直接赋值 # p2 = p1 # print(p2) # p2.name = '小白' # print(p2) # print(p1) # 2.拷贝:不管怎么拷贝,都是复制原数据产生一个新的数据并且将新数据地址返回 p1 = Person('小明') print('p1',p1) p3 = copy(p1) p4 = deepcopy(p1) print('p3',p3) print('p4',p4) # 3.深浅拷贝的取别 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)
    Processed: 0.010, SQL: 9