Python中赋值,浅拷贝,深拷贝理解(不适合新手)

亲手实践证明深拷贝确实是要多深拷贝多深,剩下的请读者看看我的实践

(base) afu@debian:~$ ipython
Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.2 -- An enhanced Interactive Python. Type '?' for help.

In [1]: a = [1, 2, 3]                                                                                 

In [2]: b = a                                                                                         

In [3]: id(a)                                                                                         
Out[3]: 139679318702832

In [4]: id(b)                                                                                         
Out[4]: 139679318702832

In [5]: id(a[0])                                                                                      
Out[5]: 94684985254688

In [6]: id(b[0])                                                                                      
Out[6]: 94684985254688

In [7]: b[0] = 4                                                                                      

In [8]: a[0]                                                                                          
Out[8]: 4

In [9]: id(b[0])                                                                                      
Out[9]: 94684985254784

In [10]: id(a[0])                                                                                     
Out[10]: 94684985254784

In [11]: import copy                                                                                  

In [12]: c = [[1, 2], 3]                                                                              

In [13]: d = copy.copy(c)                                                                             

In [14]: id(c)                                                                                        
Out[14]: 139679318592320

In [15]: id(d)                                                                                        
Out[15]: 139679318613280

In [16]: id(c[0])                                                                                     
Out[16]: 139679318590400

In [17]: id(d[0])                                                                                     
Out[17]: 139679318590400

In [18]: c[1] = 4                                                                                     

In [19]: d[1]                                                                                         
Out[19]: 3

In [20]: c[0][0] = 5                                                                                  

In [21]: d[0][0]                                                                                      
Out[21]: 5

In [22]: e = [[[[[1, 2], 3], 4] ,5], 6]                                                               

In [23]: f = copy.deepcopy(e)                                                                         

In [24]: id(e)                                                                                        
Out[24]: 139679318698976

In [25]: id(f)                                                                                        
Out[25]: 139679318770304

In [26]: id(e[0])                                                                                     
Out[26]: 139679318607344

In [27]: id(f[0])                                                                                     
Out[27]: 139679320535360

In [28]: id(e[0][0])                                                                                  
Out[28]: 139679318672560

In [29]: id(f[0][0])                                                                                  
Out[29]: 139679318737776

In [30]: id(e[0][0][0])                                                                               
Out[30]: 139679318138064

In [31]: id(f[0][0][0])                                                                               
Out[31]: 139679319122000

In [32]: id(e[0][0][0][0])                                                                            
Out[32]: 139679318283520

In [33]: id(f[0][0][0][0])                                                                            
Out[33]: 139679318575696

In [34]: id(e[0][0][0][0][0])                                                                         
Out[34]: 94684985254688

In [35]: id(f[0][0][0][0][0])                                                                         
Out[35]: 94684985254688

In [36]: e[0][0][0][0][0]                                                                             
Out[36]: 1

In [37]: f[0][0][0][0][0]=9                                                                           

In [38]: e[0][0][0][0][0]                                                                             
Out[38]: 1

In [39]: id(f[0][0][0][0][0])                                                                         
Out[39]: 94684985254944

In [40]: id(e[0][0][0][0][0])                                                                         
Out[40]: 94684985254688

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值