Python 淺拷貝與深拷貝 再次整理

2021-09-01 18:27:02 字數 4707 閱讀 7347

# -*- coding: utf-8 -*-

import copy

def print_id(lst):

lst_id = [id(y) for y in lst]

lst_id.extend([id(y) for y in lst[1]])

return str(lst_id)

def test_copy():

anndy = ['anndy', ['age', 24]]

tom = anndy[:]

cindy = list(anndy)

# 列印anndy, tom, cindy容器元素的id

print "anndy: " + str(anndy)

print "tom: " + str(tom)

print "cindy: " + str(cindy)

print 'anndy id: ' + print_id(anndy)

print 'tom id: ' + print_id(tom)

print 'cindy id: ' + print_id(cindy)

print '***修改不可變的變數值,名字***'

tom[0] = 'tom'

cindy[0] = 'cindy'

print "anndy: " + str(anndy)

print "tom: " + str(tom)

print "cindy: " + str(cindy)

print 'anndy id: ' + print_id(anndy)

print 'tom id: ' + print_id(tom)

print 'cindy id: ' + print_id(cindy)

print "以上說明,修改不可變的變數值,不會影響其他物件"

print '***修改可變的變數的值,改年齡為48***'

tom[1][1] = 48

print "anndy: " + str(anndy)

print "tom: " + str(tom)

print "cindy: " + str(cindy)

print 'anndy id: ' + print_id(anndy)

print 'tom id: ' + print_id(tom)

print 'cindy id: ' + print_id(cindy)

print "以上說明,修改可變的變數的值,會影響其他物件的值,用不好這是會造成很大影響的"

print "***深拷貝可以解決這個問題,如下:***"

anndy = ['anndy', ['age', 24]]

tom = copy.deepcopy(anndy)

cindy = copy.deepcopy(anndy)

print "anndy: " + str(anndy)

print "tom: " + str(tom)

print "cindy: " + str(cindy)

# print '列印anndy, tom, cindy容器元素的id'

print 'anndy id: ' + print_id(anndy)

print 'tom id: ' + print_id(tom)

print 'cindy id: ' + print_id(cindy)

print '修改tom的年齡的值為68'

tom[1][1] = 68

print "anndy: " + str(anndy)

print "tom: " + str(tom)

print "cindy: " + str(cindy)

# print '列印anndy, tom, cindy容器元素的id'

print 'anndy id: ' + print_id(anndy)

print 'tom id: ' + print_id(tom)

print 'cindy id: ' + print_id(cindy)

print '此次修改tom的年齡沒有影響到anndy和cindy的年齡,同時觀察anndy和cindy的年齡的引用沒變,但是tom的年齡的引用的id變了指向了物件68的位址'

if __name__ == '__main__':

test_copy()

以上test_copy()的執行結果:

ssh: -u /home/wfq/ops/test/my_copy.py

anndy: [『anndy』, [『age』, 24]]

tom: [『anndy』, [『age』, 24]]

cindy: [『anndy』, [『age』, 24]]

anndy id: [140123118376256, 140123118857824, 140123118518064, 16486192]

tom id: [140123118376256, 140123118857824, 140123118518064, 16486192]

cindy id: [140123118376256, 140123118857824, 140123118518064, 16486192]

修改不可變的變數值,名字

anndy: [『anndy』, [『age』, 24]]

tom: [『tom』, [『age』, 24]]

cindy: [『cindy』, [『age』, 24]]

anndy id: [140123118376256, 140123118857824, 140123118518064, 16486192]

tom id: [140123118518104, 140123118857824, 140123118518064, 16486192] – tom的名字的引用位址改變了,指向了物件』tom』

cindy id: [140123118376352, 140123118857824, 140123118518064, 16486192] – cindy的名字的引用位址改變了,指向了物件』cindy』

名字為字串,為不可變數,修改的時候會重新建立物件,僅僅包括原子物件的元組也屬於這種情況,那麼這樣沒有問題,還能節省記憶體

以上說明,修改不可變的變數值,不會影響其他物件

修改可變的變數的值,改年齡為48

anndy: [『anndy』, [『age』, 48]]

tom: [『tom』, [『age』, 48]]

cindy: [『cindy』, [『age』, 48]]

anndy id: [140123118376256, 140123118857824, 140123118518064,16487608]

tom id: [140123118518104, 140123118857824, 140123118518064,16487608]

cindy id: [140123118376352, 140123118857824, 140123118518064,16487608]

以上說明,修改可變的變數的值,會影響其他物件的值,用不好這是會造成很大影響的

年齡這個list是乙個可變變數,修改他的值24為48的時候,這個list本身的引用位址140123118857824沒變,但是裡邊的值從指向到24物件的位址16486192改指向到了48這個物件的位址16487608

深拷貝可以解決這個問題,如下:

anndy: [『anndy』, [『age』, 24]]

tom: [『anndy』, [『age』, 24]]

cindy: [『anndy』, [『age』, 24]]

anndy id: [140123118376256, 140123118857536, 140123118518064, 16486192]

tom id: [140123118376256, 140123118388504, 140123118518064, 16486192]

cindy id: [140123118376256, 140123118388576, 140123118518064, 16486192]

修改tom的年齡的值為68

anndy: [『anndy』, [『age』, 24]]

tom: [『anndy』, [『age』, 68]]

cindy: [『anndy』, [『age』, 24]]

anndy id: [140123118376256, 140123118857536, 140123118518064, 16486192]

tom id: [140123118376256, 140123118388504, 140123118518064,16487128]

cindy id: [140123118376256, 140123118388576, 140123118518064, 16486192]

此次修改tom的年齡沒有影響到anndy和cindy的年齡,同時觀察anndy和cindy的年齡的引用沒變,但是tom的年齡的引用的id變了指向了物件68的位址16487128

process finished with exit code 0

淺拷貝 深拷貝的再次理解

以前對淺拷貝的認識不夠,認為只是對指標的賦值,沒有真實的開闢空間。看了一篇部落格後感覺自己的認識還不夠,其實淺拷貝還會導致記憶體洩漏!對,你沒看錯,確實是 記憶體洩漏!最後會把部落格鏈結貼出來。我們以前對淺拷貝深拷貝的理解是這樣的 淺拷貝是對指標的拷貝,拷貝以後兩個指標指向的是同一塊記憶體空間,深拷...

Python 淺拷貝與深拷貝

淺拷貝 構造方法或切片 做的是淺拷貝 即拷貝了最外層容器,副本中的元素是原容器中元素的引用 在 python 中,通過乙個物件向另外乙個物件賦值,實際僅僅是賦值了物件的引用,而非建立乙個物件並賦值。那如何真正拷貝物件呢?我們看一下兩種不同的拷貝方式。先從乙個示例看起 anndy anndy age ...

Python 淺拷貝與深拷貝

以下例項是使用 copy 模組的 copy.copy 淺拷貝 和 copy.deepcopy usr bin python coding utf 8 import copy a 1,2,3,4,a b 原始物件 b a 賦值,傳物件的引用 c copy.copy a 物件拷貝,淺拷貝 d copy....