日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
解密Pythonlist深/淺拷貝原理

[[422995]]

1. python list的深/淺拷貝

python 有一種常用數(shù)據(jù)類型:list,使用list時經(jīng)常需要考慮一件事件,那就是:淺拷貝與深拷貝。

成都創(chuàng)新互聯(lián)作為成都網(wǎng)站建設公司,專注網(wǎng)站建設公司、網(wǎng)站設計,有關企業(yè)網(wǎng)站建設方案、改版、費用等問題,行業(yè)涉及酒店設計等多個領域,已為上千家企業(yè)服務,得到了客戶的尊重與認可。

至于什么是深淺拷貝,先從一個示例代碼來分析一下:

 
 
 
 
  1. import copy 
  2.  
  3. # list 測試使用的源數(shù)據(jù) 
  4. lists = [[1, 2, 3], 4, 5, 6] 
  5.  
  6. def low_copy(): 
  7.     # list 淺拷貝 
  8.     low_list = copy.copy(lists) 
  9.     return list(low_list) 
  10.  
  11. def deep_copy(): 
  12.     # list 深拷貝 
  13.     deep_list = copy.deepcopy(lists) 
  14.     return list(deep_list) 
  15.  
  16. if __name__ == "__main__": 
  17.     print("源 list:", lists) 
  18.     #  分別獲取 淺拷貝、深拷貝 list對象 
  19.     lists_c = low_copy() 
  20.     lists_d = deep_copy() 
  21.     print("淺拷貝 list:", lists_c) 
  22.     print("深拷貝 list:", lists_c) 
  23.  
  24.     print("========================") 
  25.     # 對源數(shù)據(jù)的 第0下數(shù)據(jù)追加數(shù)值7 
  26.     print("對源list的第0下數(shù)據(jù)追加數(shù)值7,start") 
  27.     lists[0].append(7) 
  28.     print("對源list的第0下數(shù)據(jù)追加數(shù)值7,end") 
  29.     print("========================") 
  30.  
  31.     # 源數(shù)據(jù)的 第0下數(shù)據(jù)追加數(shù)值7 之后驗證,深淺拷貝數(shù)據(jù)的變化 
  32.     print("源 list:", lists) 
  33.     print("淺拷貝 list:", lists_c) 
  34.     print("深拷貝 list:", lists_d) 
  35.  
  36.     # 執(zhí)行結(jié)果 
  37.     #  
  38.     # 源 list: [[1, 2, 3], 4, 5, 6] 
  39.     # 淺拷貝 list: [[1, 2, 3], 4, 5, 6] 
  40.     # 深拷貝 list: [[1, 2, 3], 4, 5, 6] 
  41.  
  42.     # ======================== 
  43.     # 對源list的第0下數(shù)據(jù)追加數(shù)值7,start 
  44.     # 對源list的第0下數(shù)據(jù)追加數(shù)值7,end 
  45.     # ======================== 
  46.  
  47.     # 源 list: [[1, 2, 3, 7], 4, 5, 6] 
  48.     # 淺拷貝 list: [[1, 2, 3, 7], 4, 5, 6] 
  49.     # 深拷貝 list: [[1, 2, 3], 4, 5, 6] 

通過示例代碼可以看出:在對list進行淺拷貝、深拷貝之后,對源數(shù)據(jù)進行修改,則會直接影響淺拷貝的數(shù)據(jù),深拷貝的數(shù)據(jù)則無影響。

這說明了什么,具體又是怎么實現(xiàn)的呢?

2. pyhton list 的實現(xiàn)

首先,要說明幾點:

  1. python 底層源碼使用C語言實現(xiàn)
  2. 在 python 中一切皆對象(整數(shù)、字符串,甚至類型、函數(shù)等都是對象)

python的對象,大概分為以下幾種:

參考 https://flaggo.github.io/python3-source-code-analysis/objects/object/

  • Fundamental 對象: 類型對象
  • Numeric 對象: 數(shù)值對象
  • Sequence 對象: 容納其他對象的序列集合對象
  • Mapping 對象: 類似 C++中的 map 的關聯(lián)對象
  • Internal 對象: Python 虛擬機在運行時內(nèi)部使用的對象

3. list 對象

在python的源碼實現(xiàn)中,list的結(jié)構(gòu)體如下:

 
 
 
 
  1. // 源文件:Include/listobject.h 
  2. // listobject.h 
  3.  
  4. typedefstruct { 
  5.     // 對象的公共頭部 
  6.     PyObject_VAR_HEAD 
  7.      
  8.     // 指向 list 元素的指針向量,list[0] 就是 ob_item[0] 
  9.     // 可以看到 ob_item 是個二級指針 
  10.     //   也就是說 **ob_item 表示它是指向 PyObject類型指針數(shù)組 指針 
  11.     //      *ob_item 表示它是 PyObject類型指針數(shù)組 
  12.     /* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */ 
  13.     PyObject **ob_item; 
  14.  
  15.     /* ob_item contains space for 'allocated' elements.  The number 
  16.      * currently in use is ob_size. 
  17.      * Invariants: 
  18.      *     0 <= ob_size <= allocated 
  19.      *     len(list) == ob_size 
  20.      *     ob_item == NULL implies ob_size == allocated == 0 
  21.      * list.sort() temporarily sets allocated to -1 to detect mutations. 
  22.      * 
  23.      * Items must normally not be NULL, except during construction when 
  24.      * the list is not yet visible outside the function that builds it. 
  25.      */ 
  26.  
  27.     // list 容納元素的總數(shù) 
  28.     Py_ssize_t allocated; 
  29. } PyListObject; 

從 list 的結(jié)構(gòu)體可以看出,真正存儲對象的是 ob_item 字段,該字段是一個指向 指針數(shù)組 的指針,從而得知 PyListObject 結(jié)構(gòu)體是一個多級結(jié)構(gòu)體。

創(chuàng)建list的過程主要分為兩個步驟:

  1. 創(chuàng)建 PyListObject 結(jié)構(gòu)體
  2. 對 ob_item 指向的指針數(shù)組進行初始化操作
 
 
 
 
  1. // 源文件位置:Objects/listobject.c 
  2. // 創(chuàng)建一個新的 list 
  3. PyObject * 
  4. PyList_New(Py_ssize_t size) { 
  5.     // 判斷創(chuàng)建 list 時的 size 是否合法 
  6.     if (size < 0) { 
  7.         PyErr_BadInternalCall(); 
  8.         returnNULL; 
  9.     } 
  10.  
  11.     struct _Py_list_state *state = get_list_state(); 
  12.     // 最終創(chuàng)建的 list 對象指針 
  13.     PyListObject *op; 
  14.  
  15. #ifdef Py_DEBUG 
  16.     // PyList_New() must not be called after _PyList_Fini() 
  17.     assert(state->numfree != -1); 
  18. #endif 
  19.  
  20.     if (state->numfree) { 
  21.         state->numfree--; 
  22.         op = state->free_list[state->numfree]; 
  23.         _Py_NewReference((PyObject *) op); 
  24.     } else { 
  25.         // 創(chuàng)建一個新的 list 
  26.         op = PyObject_GC_New(PyListObject, &PyList_Type); 
  27.         if (op == NULL) { 
  28.             returnNULL; 
  29.         } 
  30.     } 
  31.  
  32.     if (size <= 0) { 
  33.         op->ob_item = NULL; 
  34.     } else { 
  35.         op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *)); 
  36.         if (op->ob_item == NULL) { 
  37.             Py_DECREF(op); 
  38.             return PyErr_NoMemory(); 
  39.         } 
  40.     } 
  41.  
  42.     Py_SET_SIZE(op, size); 
  43.     op->allocated = size; 
  44.     _PyObject_GC_TRACK(op); 
  45.     return (PyObject *) op; 

4. list 淺拷貝

 
 
 
 
  1. // 源文件位置:Objects/listobject.c 
  2.  
  3. /*[clinic input] 
  4. list.copy 
  5. Return a shallow copy of the list. 
  6. [clinic start generated code]*/ 
  7.  
  8. // list 的 淺拷貝 
  9. static PyObject * 
  10. list_copy_impl(PyListObject *self) 
  11. /*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/ 
  12.     return list_slice(self, 0, Py_SIZE(self)); 
  13.  
  14.  
  15. // ilow、ihigh 的類型 Py_ssize_t 為當前系統(tǒng)一個指針的大小 
  16. static PyObject * 
  17. list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) { 
  18.     PyListObject *np; 
  19.     PyObject **src, **dest; 
  20.     Py_ssize_t i, len; 
  21.     len = ihigh - ilow; 
  22.     if (len <= 0) { 
  23.         return PyList_New(0); 
  24.     } 
  25.  
  26.     // 生成新的 list 
  27.     np = (PyListObject *) list_new_prealloc(len); 
  28.     if (np == NULL) 
  29.         returnNULL; 
  30.  
  31.     // 從 list 的第一個位置開始 a->ob_item 偏移 ilow,即:移動到 第 ilow 個數(shù)值元素的指針位置 
  32.     src = a->ob_item + ilow; 
  33.  
  34.     // 新的 list 的 數(shù)值列表第一個位置 
  35.     dest = np->ob_item; 
  36.  
  37.     // 進行復制,注意:只是復制了 對象的指針 
  38.     for (i = 0; i < len; i++) { 
  39.         // src[i] 存儲著 指向具體的對象的指針 
  40.         PyObject *v = src[i]; 
  41.  
  42.         // v 的引用計數(shù) +1 
  43.         Py_INCREF(v); 
  44.  
  45.         // 復制到新的list中 
  46.         // 此時 新老list底層數(shù)據(jù)對象指向相同 
  47.         dest[i] = v; 
  48.     } 
  49.  
  50.     // 設置新list的size 
  51.     // ob->ob_size = size 
  52.     Py_SET_SIZE(np, len); 
  53.     return (PyObject *) np; 

 進行淺拷貝之后,從內(nèi)存布局發(fā)生的變化,可以看出:新、老list共享底層數(shù)據(jù)對象,這也是導致一個list進行修改之后,影響其他list的原因。

5. list 深拷貝

進行深拷貝之后,從內(nèi)存布局發(fā)生的變化,可以看出:新、老list分別使用不同的底層數(shù)據(jù)對象,這就不會導致一個list進行修改之后,影響其他list。

總結(jié)

通過分析python底層源碼了解到list的底層結(jié)構(gòu)以及深、淺拷貝原理,開發(fā)過程中使用深拷貝還是淺拷貝,則需要根據(jù)實際情況來處理。

  • 淺拷貝在拷貝時,只拷貝第一層中的引用,如果元素是可變對象,并且被修改,那么拷貝的對象也會發(fā)生變化。
  • 深拷貝在拷貝時,會逐層進行拷貝,直到所有的引用都是不可變對象為止。
  • Python 有多種方式實現(xiàn)淺拷貝,copy 模塊的 copy 函數(shù) ,對象的 copy 函數(shù) ,工廠方法,切片等。
  • 大多數(shù)情況下,編寫程序時,都是使用淺拷貝,除非有特定的需求。
  • 淺拷貝的優(yōu)點:拷貝速度快,占用空間少,拷貝效率高。

當前名稱:解密Pythonlist深/淺拷貝原理
當前地址:http://www.dlmjj.cn/article/dpsoppd.html