python深浅拷贝

Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果。

下面本文就通过简单的例子介绍一下这些概念之间的差别。

  • 要想了解深浅拷贝,首先要知道什么是深浅拷贝?

  • 深拷贝是对于一个对象所有层次的拷贝(递归)copy.deepcopy


        浅拷贝是对于一个对象的顶层拷贝;通俗的理解是:拷贝了引用,并没有拷贝内容.copy.copy

    #    要想知道两者的区别,首先要知道什么是可变类型和不可变类型?
        
        不可变类型:整型,长整型,浮点数,复数,布尔,字符串,元组
        可变类型:列表,字典。

    #    深浅拷贝有什么区别那?
        
        1、如果用copy.copy、copy.deepcopy对一个全部都是不可变类型的数据进行拷贝,那么它们结果相同,都是引用指向;

        2、如果拷贝的是一个拥有不可变类型的数据,即使元组是最顶层,那么deepcopy依然是深拷贝,而copy.copy还是指向

        3、基本上只要不是我们自已手动调用的deepcopy方法都是浅拷贝,切片拷贝字典拷贝都是浅拷贝,而有些内置函数可以生成拷贝(list),属于深拷贝:a = list(range(10))
    b = list(a) 

  • 预备知识——各基本数据类型的地址存储及改变情况

      在python中的数据类型包括:bool、int、long、float、str、set、list、tuple、dict等等。我们可以大致将这些数据类型归类为简单数据类型和复杂的数据结构。

  • 我的划分标准是,如果一个数据类型,可以将其他的数据类型作为自己的元素,我就认为这是一种数据结构。数据结构的分类有很多种,但是在Python中常用的只有集合、序列和映射三种结构。对应python中的set、list(tuple、str)、dict;常用的数据类型有int、long、float、bool、str等类型。(其中,str类型比较特别,因为从C语言的角度来说,str其实是一个char的集合,但是这与本文的关联不大,所以我们暂时不谈这个问题)

  •   

      由于python中的变量都是采用的引用语义,数据结构可以包含基础数据类型,导致了在python中数据的存储是下图这种情况,每个变量中都存储了这个变量的地址,而不是值本身;对于复杂的数据结构来说,里面的存储的也只只是每个元素的地址而已。:

      

      1.数据类型重新初始化对python语义引用的影响

      变量的每一次初始化,都开辟了一个新的空间,将新内容的地址赋值给变量。对于下图来说,我们重复的给str1赋值,其实在内存中的变化如下右图:

               

      从上图我们可以看出,str1在重复的初始化过程中,是因为str1中存储的元素地址由'hello world'的地址变成了'new hello world'的。

      2.数据结构内部元素变化重对python语义引用的影响

      对于复杂的数据类型来说,改变其内部的值对于变量的影响:

               

      当对列表中的元素进行一些增删改的操作的时候,是不会影响到lst1列表本身对于整个列表地址的,只会改变其内部元素的地址引用。可是当我们对于一个列表重新初始化(赋值)的时候,就给lst1这个变量重新赋予了一个地址,覆盖了原本列表的地址,这个时候,lst1列表的内存id就发生了改变。上面这个道理用在所有复杂的数据类型中都是一样的

  • 变量的赋值

      搞明白了上面的内容,再来探讨变量的赋值,就变得非常简单了。

  • print("变量的赋值")
    str1 = "hello world"
    print(str1)
    print(id(str1))   # 36148592
    str2 = str1
    print(id(str2)) # 36148592
    str1 = "new world"
    print(str1)
    print(str2)
    print(id(str1))  # 36206832
    print(id(str2))  # 36148592

      1.str的赋值

                 

      我们刚刚已经知道,str1的再次初始化(赋值)会导致内存地址的改变,从上图的结果我们可以看出修改了str1之后,被赋值的str2从内存地址到值都没有受到影响。

      看内存中的变化,起始的赋值操作让str1和str2变量都存储了‘hello world’所在的地址,重新对str1初始化,使str1中存储的地址发生了改变,指向了新建的值,此时str2变量存储的内存地址并未改变,所以不受影响。

    2.复杂的数据结构中的赋值

      刚刚我们看了简单数据类型的赋值,现在来看复杂数据结构变化对应内存的影响。

  • print("复杂的数据结构中的赋值")
    list1 = [1, 2, 3, 4, 5, 6]
    list2 = list1
    
    print(id(list1)) # 42367240
    print(id(list2)) # 42367240
    list1.append('new item')
    print(list1) # [1, 2, 3, 4, 5, 6, 'new item']
    print(list2) # [1, 2, 3, 4, 5, 6, 'new item']
    print(id(list1)) # 42367240
    print(id(list2)) # 42367240

               

      上图对列表的增加修改操作,没有改变列表的内存地址,lst1和lst2都发生了变化。

      对照内存图我们不难看出,在列表中添加新值时,列表中又多存储了一个新元素的地址,而列表本身的地址没有变化,所以lst1和lst2的id均没有改变并且都被添加了一个新的元素。

      简单的比喻一下,我们出去吃饭,lst1和lst2就像是同桌吃饭的两个人,两个人公用一张桌子,只要桌子不变,桌子上的菜发生了变化两个人是共同感受的。

    初识拷贝

      我们已经详细了解了变量赋值的过程。对于复杂的数据结构来说,赋值就等于完全共享了资源,一个值的改变会完全被另一个值共享。

      然而有的时候,我们偏偏需要将一份数据的原始内容保留一份,再去处理数据,这个时候使用赋值就不够明智了。python为这种需求提供了copy模块。提供了两种主要的copy方法,一种是普通的copy,另一种是deepcopy。我们称前者是浅拷贝,后者为深拷贝。

      深浅拷贝一直是所有编程语言的重要知识点,下面我们就从内存的角度来分析一下两者的区别。

    浅拷贝

      首先,我们来了解一下浅拷贝。浅拷贝:不管多么复杂的数据结构,浅拷贝都只会copy一层。下面就让我们看一张图,来了解一下浅浅拷贝的概念。

       

          看上面两张图,我们加入左图表示的是一个列表sourcelist,sourcelist = ['str1','str2','str3','str4','str5',['str1','str2','str3','str4','str5']];

      右图在原有的基础上多出了一个浅拷贝的copylist,copylist = ['str1','str2','str3','str4','str5',['str1','str2','str3','str4','str5']];

      sourcelist和copylist表面上看起来一模一样,但是实际上在内存中已经生成了一个新列表,copy了sourceLst,获得了一个新列表,存储了5个字符串和一个列表所在内存的地址。

          我们看下面分别对两个列表进行的操作,红色的框框里面是变量初始化,初始化了上面的两个列表;我们可以分别对这两个列表进行操作,例如插入一个值,我们会发现什么呢?如下所示:

             

       从上面的代码我们可以看出,对于sourceLst和copyLst列表添加一个元素,这两个列表好像是独立的一样都分别发生了变化,但是当我修改lst的时候,这两个列表都发生了变化,这是为什么呢?我们就来看一张内存中的变化图:

             

      我们可以知道sourceLst和copyLst列表中都存储了一坨地址,当我们修改了sourceLst1的元素时,相当于用'sourceChange'的地址替换了原来'str1'的地址,所以sourceLst的第一个元素发生了变化。而copyLst还是存储了str1的地址,所以copyLst不会发生改变。

      当sourceLst列表发生变化,copyLst中存储的lst内存地址没有改变,所以当lst发生改变的时候,sourceLst和copyLst两个列表就都发生了改变。

      这种情况发生在字典套字典、列表套字典、字典套列表,列表套列表,以及各种复杂数据结构的嵌套中,所以当我们的数据类型很复杂的时候,用copy去进行浅拷贝就要非常小心。。。

  • print("浅拷贝")
    import copy
    lst = ['str1', 'str2', 'str3']
    sourcelst = ['str1', 'str2', 'str3', lst]
    copylst = copy.copy(sourcelst)
    print("原本地址")
    print([id(ele) for ele in sourcelst])
    print([id(ele) for ele in copylst])
    print("当sourceLst列表发生变化,copyLst中存储的lst内存地址没有改变")
    sourcelst.append('source')
    copylst.append('copy')
    print("->sourcelst: ", sourcelst)
    print("->copylst: ", copylst)
    print(id(sourcelst))  # 4
    print(sourcelst)  # 5
    print([id(ele) for ele in sourcelst])
    print(id(copylst))  # 4
    print(copylst)  # 5
    print([id(ele) for ele in copylst])
    print("sourceLst的第一个元素发生了变化。而copyLst还是存储了str1的地址,所以copyLst不会发生改变。")
    print([id(ele) for ele in sourcelst])
    print([id(ele) for ele in copylst])
    sourcelst[0] = 'change'
    print("->sourcelst: ", sourcelst)
    print("->copylst: ", copylst)
    print(id(sourcelst))  # 4
    print(sourcelst)  # 5
    print([id(ele) for ele in sourcelst])
    print(id(copylst))  # 4
    print(copylst)  # 5
    print([id(ele) for ele in copylst])
    
    print("以当lst发生改变的时候,sourceLst和copyLst两个列表就都发生了改变。")
    print([id(ele) for ele in sourcelst])
    print([id(ele) for ele in copylst])
    lst.append('Append')
    print("->sourcelst: ", sourcelst)
    print("->copylst: ", copylst)
    print(id(sourcelst))  # 4
    print(sourcelst)  # 5
    print([id(ele) for ele in sourcelst])
    print(id(copylst))  # 4
    print(copylst)  # 5
    print([id(ele) for ele in copylst])
    #####################################################################################3
    C:\Anaconda3\python.exe H:/python/venv/pizza.py
    浅拷贝
    原本地址
    [40600440, 42294440, 42294664, 42394568]
    [40600440, 42294440, 42294664, 42394568]
    当sourceLst列表发生变化,copyLst中存储的lst内存地址没有改变
    ->sourcelst:  ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
    ->copylst:  ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'copy']
    42394504
    ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
    [40600440, 42294440, 42294664, 42394568, 30438600]
    42394376
    ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'copy']
    [40600440, 42294440, 42294664, 42394568, 4282776]
    sourceLst的第一个元素发生了变化。而copyLst还是存储了str1的地址,所以copyLst不会发生改变。
    [40600440, 42294440, 42294664, 42394568, 30438600]
    [40600440, 42294440, 42294664, 42394568, 4282776]
    ->sourcelst:  ['change', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
    ->copylst:  ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'copy']
    42394504
    ['change', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
    [42404472, 42294440, 42294664, 42394568, 30438600]
    42394376
    ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'copy']
    [40600440, 42294440, 42294664, 42394568, 4282776]
    以当lst发生改变的时候,sourceLst和copyLst两个列表就都发生了改变。
    [42404472, 42294440, 42294664, 42394568, 30438600]
    [40600440, 42294440, 42294664, 42394568, 4282776]
    ->sourcelst:  ['change', 'str2', 'str3', ['str1', 'str2', 'str3', 'Append'], 'source']
    ->copylst:  ['str1', 'str2', 'str3', ['str1', 'str2', 'str3', 'Append'], 'copy']
    42394504
    ['change', 'str2', 'str3', ['str1', 'str2', 'str3', 'Append'], 'source']
    [42404472, 42294440, 42294664, 42394568, 30438600]
    42394376
    ['str1', 'str2', 'str3', ['str1', 'str2', 'str3', 'Append'], 'copy']
    [40600440, 42294440, 42294664, 42394568, 4282776]
    
    Process finished with exit code 0
    ###################################################################################

    深拷贝

      刚刚我们了解了浅拷贝的意义,但是在写程序的时候,我们就是希望复杂的数据结构之间完全copy一份并且它们之间又没有一毛钱关系,应该怎么办呢?

      我们引入一个深拷贝的概念,深拷贝——即python的copy模块提供的另一个deepcopy方法。深拷贝会完全复制原变量相关的所有数据,在内存中生成一套完全一样的内容,在这个过程中我们对这两个变量中的一个进行任意修改都不会影响其他变量。下面我们就来试验一下。

           

          看上面的执行结果,这一次我们不管是对直接对列表进行操作还是对列表内嵌套的其他数据结构操作,都不会产生拷贝的列表受影响的情况。我们再来看看这些变量在内存中的状况:

         

      看了上面的内容,我们就知道了深拷贝的原理。其实深拷贝就是在内存中重新开辟一块空间,不管数据结构多么复杂,只要遇到可能发生改变的数据类型,就重新开辟一块内存空间把内容复制下来,直到最后一层,不再有复杂的数据类型,就保持其原引用。这样,不管数据结构多么的复杂,数据之间的修改都不会相互影响。这就是深拷贝~~~

  • print("深拷贝")
    import copy
    lst = ['str1', 'str2', 'str3']
    sourcelst = ['str1', 'str2', 'str3', lst]
    deepcopylst = copy.deepcopy(sourcelst)
    print("原本地址")
    print([id(ele) for ele in sourcelst])
    print([id(ele) for ele in deepcopylst])
    print("当sourceLst列表发生变化,copyLst中存储的lst内存地址没有改变")
    sourcelst.append('source')
    deepcopylst.append('deepcopy')
    print("->sourcelst: ", sourcelst)
    print("->deepcopylst: ", deepcopylst)
    print(id(sourcelst))  # 4
    print(sourcelst)  # 5
    print([id(ele) for ele in sourcelst])
    print(id(deepcopylst))  # 4
    print(deepcopylst)  # 5
    print([id(ele) for ele in deepcopylst])
    print("sourceLst的第一个元素发生了变化。而copyLst还是存储了str1的地址,所以copyLst不会发生改变。")
    print([id(ele) for ele in sourcelst])
    print([id(ele) for ele in deepcopylst])
    sourcelst[0] = 'change'
    print("->sourcelst: ", sourcelst)
    print("->deepcopylst: ", deepcopylst)
    print(id(sourcelst))  # 4
    print(sourcelst)  # 5
    print([id(ele) for ele in sourcelst])
    print(id(deepcopylst))  # 4
    print(deepcopylst)  # 5
    print([id(ele) for ele in deepcopylst])
    
    print("以当lst发生改变的时候,sourceLst和copyLst两个列表就都发生了改变。")
    print([id(ele) for ele in sourcelst])
    print([id(ele) for ele in deepcopylst])
    lst.append('Append')
    print("->sourcelst: ", sourcelst)
    print("->copylst: ", deepcopylst)
    print(id(sourcelst))  # 4
    print(sourcelst)  # 5
    print([id(ele) for ele in sourcelst])
    print(id(deepcopylst))  # 4
    print(deepcopylst)  # 5
    print([id(ele) for ele in deepcopylst])
    ##################################################################################
    C:\Anaconda3\python.exe H:/python/venv/pizza.py
    浅拷贝
    原本地址
    [32015224, 36265128, 36265352, 36365384]
    [32015224, 36265128, 36265352, 36364616]
    当sourceLst列表发生变化,copyLst中存储的lst内存地址没有改变
    ->sourcelst:  ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
    ->deepcopylst:  ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'deepcopy']
    36365320
    ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
    [32015224, 36265128, 36265352, 36365384, 30504136]
    36365192
    ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'deepcopy']
    [32015224, 36265128, 36265352, 36364616, 35978480]
    sourceLst的第一个元素发生了变化。而copyLst还是存储了str1的地址,所以copyLst不会发生改变。
    [32015224, 36265128, 36265352, 36365384, 30504136]
    [32015224, 36265128, 36265352, 36364616, 35978480]
    ->sourcelst:  ['change', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
    ->deepcopylst:  ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'deepcopy']
    36365320
    ['change', 'str2', 'str3', ['str1', 'str2', 'str3'], 'source']
    [36375048, 36265128, 36265352, 36365384, 30504136]
    36365192
    ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'deepcopy']
    [32015224, 36265128, 36265352, 36364616, 35978480]
    以当lst发生改变的时候,sourceLst和copyLst两个列表就都发生了改变。
    [36375048, 36265128, 36265352, 36365384, 30504136]
    [32015224, 36265128, 36265352, 36364616, 35978480]
    ->sourcelst:  ['change', 'str2', 'str3', ['str1', 'str2', 'str3', 'Append'], 'source']
    ->copylst:  ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'deepcopy']
    36365320
    ['change', 'str2', 'str3', ['str1', 'str2', 'str3', 'Append'], 'source']
    [36375048, 36265128, 36265352, 36365384, 30504136]
    36365192
    ['str1', 'str2', 'str3', ['str1', 'str2', 'str3'], 'deepcopy']
    [32015224, 36265128, 36265352, 36364616, 35978480]
    
    Process finished with exit code 0
    #######################################################################################

    对象赋值

直接看一段代码:

import copy

will = ["Will", 28, ["Python", "C#", "JavaScript"]]
# wilber = copy.deepcopy(will)
wilber = will

print(id(will)) # 1
print(will)  # 2
print([id(ele) for ele in will]) # 3
print(id(wilber))  # 4
print(wilber)  # 5
print([id(ele) for ele in wilber])
print("\n")
will[0] = "Wilber"
will[2].append("CSS")
print(id(will)) # 6
print(will)
print([id(ele) for ele in will])
print(id(wilber))
print(wilber)
print([id(ele) for ele in wilber])

 代码的输出为: 

C:\Anaconda3\python.exe H:/python/venv/pizza.py
42511816
['Will', 28, ['Python', 'C#', 'JavaScript']]
[31949688, 506294592, 42511880]
42511816
['Will', 28, ['Python', 'C#', 'JavaScript']]
[31949688, 506294592, 42511880]


42511816
['Wilber', 28, ['Python', 'C#', 'JavaScript', 'CSS']]
[42534368, 506294592, 42511880]
42511816
['Wilber', 28, ['Python', 'C#', 'JavaScript', 'CSS']]
[42534368, 506294592, 42511880]

Process finished with exit code 0

 下面来分析一下这段代码:

 

  • 首先,创建了一个名为will的变量,这个变量指向一个list对象,从第一张图中可以看到所有对象的地址(每次运行,结果可能不同)
    • 然后,通过will变量对wilber变量进行赋值,那么wilber变量将指向will变量对应的对象(内存地址),也就是说”wilber is will”,”wilber[i] is will[i]”

可以理解为,Python中,对象的赋值都是进行对象引用(内存地址)传递

  • 第三张图中,由于will和wilber指向同一个对象,所以对will的任何修改都会体现在wilber上

这里需要注意的一点是,str是不可变类型,所以当修改的时候会替换旧的对象,产生一个新的地址39758496

 浅拷贝

import copy

will = ["Will", 28, ["Python", "C#", "JavaScript"]]
# wilber = copy.deepcopy(will)
# wilber = will
wilber = copy.copy(will)

print(id(will)) # 1
print(will)  # 2
print([id(ele) for ele in will]) # 3
print(id(wilber))  # 4
print(wilber)  # 5
print([id(ele) for ele in wilber])
print("")
will[0] = "Wilber"
will[2].append("CSS")
print(id(will)) # 6
print(will)
print([id(ele) for ele in will])
print(id(wilber))
print(wilber)
print([id(ele) for ele in wilber])
C:\Anaconda3\python.exe H:/python/venv/pizza.py
42380744
['Will', 28, ['Python', 'C#', 'JavaScript']]
[35423096, 506294592, 42380808]
42380616
['Will', 28, ['Python', 'C#', 'JavaScript']]
[35423096, 506294592, 42380808]

42380744
['Wilber', 28, ['Python', 'C#', 'JavaScript', 'CSS']]
[42403352, 506294592, 42380808]
42380616
['Will', 28, ['Python', 'C#', 'JavaScript', 'CSS']]
[35423096, 506294592, 42380808]

Process finished with exit code 0

分析一下这段代码:

  • 首先,依然使用一个will变量,指向一个list类型的对象
  • 然后,通过copy模块里面的浅拷贝函数copy(),对will指向的对象进行浅拷贝,然后浅拷贝生成的新对象赋值给wilber变量

浅拷贝会创建一个新的对象,这个例子中”wilber is not will”
但是,对于对象中的元素,浅拷贝就只会使用原始元素的引用(内存地址),也就是说”wilber[i] is will[i]”

  • 当对will进行修改的时候

由于list的第一个元素是不可变类型,所以will对应的list的第一个元素会使用一个新的对象39758496

但是list的第三个元素是一个可不类型,修改操作不会产生新的对象,所以will的修改结果会相应的反应到wilber上

总结一下,当我们使用下面的操作的时候,会产生浅拷贝的效果:

  • 使用切片[:]操作
  • 使用工厂函数(如list/dir/set)
  • 使用copy模块中的copy()函数
  •  

深拷贝

最后来看看深拷贝:

import copy

will = ["Will", 28, ["Python", "C#", "JavaScript"]]
wilber = copy.deepcopy(will)

print(id(will)) # 1
print(will)  # 2
print([id(ele) for ele in will]) # 3
print(id(wilber))  # 4
print(wilber)  # 5
print([id(ele) for ele in wilber])

will[0] = "Wilber"
will[2].append("CSS")
print(id(will)) # 6
print(will)
print([id(ele) for ele in will])
print(id(wilber))
print(wilber)
print([id(ele) for ele in wilber])
C:\Anaconda3\python.exe H:/python/venv/pizza.py
42708424
['Will', 28, ['Python', 'C#', 'JavaScript']]
[35816312, 506294592, 42708488]
42708296
['Will', 28, ['Python', 'C#', 'JavaScript']]
[35816312, 506294592, 42708232]
42708424
['Wilber', 28, ['Python', 'C#', 'JavaScript', 'CSS']]
[42731032, 506294592, 42708488]
42708296
['Will', 28, ['Python', 'C#', 'JavaScript']]
[35816312, 506294592, 42708232]

Process finished with exit code 0

 分析一下这段代码:

  • 首先,同样使用一个will变量,指向一个list类型的对象
  • 然后,通过copy模块里面的深拷贝函数deepcopy(),对will指向的对象进行深拷贝,然后深拷贝生成的新对象赋值给wilber变量

 跟浅拷贝类似,深拷贝也会创建一个新的对象,这个例子中”wilber is not will”
但是,对于对象中的元素,深拷贝都会重新生成一份(有特殊情况,下面会说明),而不是简单的使用原始元素的引用(内存地址)
例子中will的第三个元素指向39737304,而wilber的第三个元素是一个全新的对象39773088,也就是说,”wilber[2] is not will[2]”

  • 当对will进行修改的时候

 由于list的第一个元素是不可变类型,所以will对应的list的第一个元素会使用一个新的对象39758496
但是list的第三个元素是一个可不类型,修改操作不会产生新的对象,但是由于”wilber[2] is not will[2]”,所以will的修改不会影响wilber

 

 

拷贝的特殊情况

其实,对于拷贝有一些特殊情况:

  • 对于非容器类型(如数字、字符串、和其他’原子’类型的对象)没有拷贝这一说

也就是说,对于这些类型,”obj is copy.copy(obj)” 、”obj is copy.deepcopy(obj)”

  • 如果元组变量只包含空列表类型对象,则不能深拷贝,看下面的例子
  • import copy
    books = ("Python", "C#", "java")
    copies = copy.deepcopy(books)
    print(books is copies)
    # 如果元组变量包含空列表类型对象,则不能深拷贝
    books = ("Python", "C#", "java", [])
    copies = copy.deepcopy(books)
    print(books is copies)
    C:\Anaconda3\python.exe H:/python/venv/pizza.py
    True
    False
    
    Process finished with exit code 0
    

    总结

    本文介绍了对象的赋值和拷贝,以及它们之间的差异:

  • Python中对象的赋值都是进行对象引用(内存地址)传递
  • 使用copy.copy(),可以进行对象的浅拷贝,它复制了对象,但对于对象中的元素,依然使用原始的引用.
  • 如果需要复制一个容器对象,以及它里面的所有元素(包含元素的子元素),可以使用copy.deepcopy()进行深拷贝
  • 对于非容器类型(如数字、字符串、和其他’原子’类型的对象)没有被拷贝一说
  • 如果元组变量只包含空列表类型对象,则不能深拷贝
  • 19
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值