浅复制和深复制

目录

浅复制(拷贝)

浅复制:如果容器中有可变对象如,列表,字典等,那么副本中存在的是源容器可变对象的引用

复制列表的两种方式:

>>> L1 = [1,2,3,4]   
>>> L2 = list(L1)    # 方式一
>>> L3 = L1[:]       # 方式二
>>> L1 == L2 == L3
True
>>> L1 is L2
False
>>> L2 is L3
False

深拷贝

深复制:副本不共享内部对象的引用,copy 模块的deepcopy()做深复制, copy()做浅复制

实例代码:

import copy

class Bus:
    def __init__(self, passengers=None):
        if passengers is None:
            self.passengers = []
        else:
            self.passengers = list(passengers)

    def pick(self, name):
        self.passengers.append(name)

    def drop(self, name):
        self.passengers.remove(name)

if __name__ == '__main__':
    bus1 = Bus(['zhangfei', 'zhugeliang', 'zhaoyun', 'liubei'])
    bus2  = copy.copy(bus1)  # 浅复制
    bus3 = copy.deepcopy(bus1) # 深复制
    print(id(bus1), id(bus2), id(bus3)) # out: 2154872061904 2154872061376 2154872056672
    bus1.drop('zhugeliang')
    print(bus2.passengers)   # out: ['zhangfei', 'zhaoyun', 'liubei']

    # out: 2847485108864 2847485108864 2847481853376
    # bus1 和 bus2 的 passengers 共享,因为浅拷贝
    print(id(bus1.passengers), id(bus2.passengers), id(bus3.passengers))
    
    print(bus3.passengers)  # out: ['zhangfei', 'zhugeliang', 'zhaoyun', 'liubei'] 没有变化

不要使用可变参数做默认值

在函数内部如果有传入可变参数,最好使用副本操作,这样不会影响外部,除非这个方法方法确实想修改通过参数传入的对象

上一篇:简述python深浅拷贝


下一篇:Python-列表加乘,嵌套列表,is运算符,浅拷贝和深拷贝,列表推导式