0
我正在使用Python深拷貝嘗試創建原始對象的全部副本,但深拷貝似乎沒有創建副本。它仍然與原始對象共享相同的參考,這是不期望的python中的深拷貝
這是代碼。我有一個class Board
,我想深入複製的實例。
import copy
class Board:
xpos = None
opos = None
count = None
status = []
def __init__(self, size):
self.xpos=[0,0]
self.opos=[size-1,size-1]
self.count = size*size-2
for i in range(size):
tmp = ['-']*size
self.status.append(tmp)
self.status[0][0] = 'X'
self.status[size-1][size-1]= 'O'
某處在其他功能我想打電話給
board=Board()
localboard=copy.deepcopy(board)
# then do modification to local board....
# but it seems the old board is also affected. This is so weird since
# I am already using deep copy.
所以,我怎麼可以創建舊板深拷貝?我不想共享任何引用,因爲我會做對當地一個修改,並希望保持舊的完整..
這與deepcopy和任何與您爲班級設置的可變類屬性無關。 – 2013-03-23 15:51:41
非常感謝,這正是我所期待的! – Daniel 2013-03-23 15:59:28